0 0
Read Time:3 Minute, 7 Second

Laravel: Controller Explanation

Controllers are another essential feature provided by Laravel. In place of defining the handling request logic in the form of Closures in route files, it is possible to organize this process with the help of Controller classes. So what the controllers do? Controllers are meant to group associated request handling logic within a single class. In your Laravel project, they are stored in the app/Http/Controllers directory. The full form of MVC is Model View Controller, which directs traffic among the Views and the Models.

Creating Controllers

Open your CMD or terminal and type the command:

Syntax:

php artisan make:controller <controller-name> --plain

Replace this <controller-name> in the above syntax with your controller. This will eventually make a plain constructor since you are passing the argument –plain.

The controller that you have created can be invoked from within the routes.php file using this syntax below-

Example:

Route::get('base URI','controller@method');

A basic controller code-snippet will look something like this, and you have to create it in the directory like app/Http/Controller/AdminController.php:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class AdminController extends Controller
{
//
}

Controller Middleware

You can assign controllers to middlewares to route in the route files of your project using the command below:

Example:

Route::get('profile', 'AdminController@show')->middleware('auth');

Middleware methods from the controller help to easily assign middleware to the controller’s action and activity. Restrictions on implementing certain methods can also be provided to middlewares on the controller class.

class AdminController extends Controller
{
    public function __construct()
    {
        // function body
    }
}

Using closures, controllers may allow laravel developers to register middleware.

$this->middleware(function ($request, $next) {
    // middleware statements;
    return $next($request);
}
);

Resource Controllers

The resource route of Laravel allows the classic “CRUD” routes for controllers to have a single line of code. This can be created quickly using the make: controller command (Artisan command) something like this”

Example:

php artisan make:controller PasswordController --resource

The above code will produce a controller in app/Http/Controllers/ location with the file name PasswordController.php which will hold a method for all available tasks of resources.

Laravel developers also have the freedom to register multiple resource controllers at a time by passing an array to a resource method, something like this:

Route::resources([
    'password' => 'PasswordController',
    'picture' => 'DpController'
]
);

Actions Handled by Resource Controllers

VerbURIActionRoute Name
GET/usersUsers listusers.index
POST/users/addAdd a new user users.add
GET/users/{user}Get user users.show
GET/users/{user}/editEdit user users.edit
PUT/users/{user}Update user users.update
DELETE/users/{user}Delete user users.destroy

Implicit Controllers

These types of controllers allow developers to define a single route for handling multiple actions within the controller. The syntax of using this is by:

Example:

Route::controller('base URI','<class-name-of-the-controller>');

It is where your Implicit controller file will get stored: app/Http/Controllers/ImplicitController.php; and will look have to script like:Advertisements

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ImplicitController extends Controller {
   public function getIndex(){
      echo 'starting method';
   }
   public function getVal($id){
      echo 'show value';
   }
   public function getAdminData(){
      echo 'admin data method';
   }   
   public function adminPassword(){
      echo 'password method';
   }
}

Constructor and Method Injection

The service container of Laravel is used for resolving all Laravel Controllers. So, controller injection lets Laravel developers type-hint the dependencies your controller may require within its constructor. On the other hand, method injection allows you to type-hint dependencies for the controller’s action method in your Laravel project.

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Comment