Laravel Controllers

Laravel Controllers

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

To create a controller in Laravel, open your terminal and use the following command:

Syntax:

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

Replace <controller-name> with your desired controller name. The --plain flag generates a basic controller without any predefined methods.

To use this controller in your routes, define it in routes/web.php as follows:

Example:

Route::get('base-uri', 'Controller@method');

A basic controller file (e.g., AdminController.php) is located in app/Http/Controllers/ and looks like this:

<?php

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

class AdminController extends Controller
{
    // Controller methods go here
}

Controller Middleware

You can assign middleware to controllers directly within route files:

Example:

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

Alternatively, middleware can be assigned within the controller itself:

class AdminController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
}

Middleware can also be registered using closures:

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

Resource Controllers

Resource controllers simplify CRUD operations. Create one using:

Example:

php artisan make:controller PasswordController --resource

This generates a controller at app/Http/Controllers/PasswordController.php with predefined methods for handling resource operations.

Multiple resource controllers can be registered at once:

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

Implicit controllers allow defining a single route for handling multiple actions.

Example:

Route::controller('base-uri', 'ImplicitController');

Implicit controller files are stored in app/Http/Controllers/ImplicitController.php:

<?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

Laravel's service container resolves all controllers, enabling constructor and method injection.

Constructor Injection:

class UserController extends Controller
{
    protected $user;
    
    public function __construct(UserService $user)
    {
        $this->user = $user;
    }
}

Method Injection:

public function show(User $user)
{
    return view('user.profile', compact('user'));
}
Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close