Laravel Tutorial Index

Routing and Controllers

In Laravel, controllers are a crucial feature that enables the organization of request-handling logic. Instead of defining closure functions in route files, grouping related logic in controller classes is possible. Controllers are responsible for grouping associated request-handling logic within a single class, which is stored in the app/Http/Controllers directory of your Laravel project. The complete form of MVC is the Model View Controller, which facilitates traffic flow between 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 the 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 assign middleware to the controller's action and activity easily. Restrictions on implementing certain methods can also be provided to middlewares on the controller class.

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

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

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

Resource Controllers

Laravel's resource route 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 the app/Http/Controllers/ location with the file name PasswordController.php, which will hold a method for all available resource tasks.

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:

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

Actions Handled by Resource Controllers

Verb URI Action Route Name
GET /users Users list users.index
POST /users/add Add a new user  users.add
GET /users/{user} Get user  users.show
GET /users/{user}/edit Edit 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>');

This is where your implicit controller file will be stored: App/Http/Controllers/ImplicitController.php. The script would look like this:

<?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 Laravel service container 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.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram