Laravel Controllers

Laravel Controllers

Controllers are another essential feature provided by Laravel, where 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 act as directing traffic among the Views and the Models.

Table of Contents
1. Creating Controllers
2. Controller Middleware
3. Resource Controllers
4. Actions Handled by Resource Controllers
5. Implicit Controllers
6. Constructor and Method Injection

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 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 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 having 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 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 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:

<?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 to type-hint the dependencies which 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.

Reactions

Post a Comment

0 Comments

close