How to Create Controller in Laravel Tutorial


Controllers are one other important feature provided by Laravel, the place instead of defining the handling request logic within the type of Closures in route files, it’s doable to prepare this process with the assistance of Controller classes. So what the controllers do? Controllers are supposed to group related request handling logic inside a single class. In your Laravel project, they’re saved within the app/Http/Controllers directory. The full type of MVC is Model View Controller, which act as directing traffic among the many Views and the Models.

In the MVC framework, the letter ‘C’ stands for Controller. It acts as a directing traffic between Views and Models. In this tutorial, we’ll study Controllers in Laravel.

Creating Controller

Open your CMD or terminal and enter the below the command:

php artisan make:controller <controller-name>

Replace this <controller-name> within the above syntax along with our controller.

The controller that we’ve got created will be invoked from inside routes.php file using this syntax below:

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

A fundamental controller code-snippet will look one thing like this, and you must create within the directory like app/Http/Controller/AdminController.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 within the route files of our project using the command below:

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

Middleware methods from the controller assist to simply assign middleware to the controller’s action and activity. Restrictions on implementing certain methods will 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 useful resource route of Laravel permits the basic “CRUD” routes for controllers having a single line of code. This will be created rapidly utilizing the make: controller command (Artisan command) one thing 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 can maintain a method for all available tasks of resources.

Laravel developers even have the liberty to register a number of useful resource controllers at a time by passing an array to useful resource methodology one thing like this:

    'password' => 'PasswordController',
    'picture' => 'DpController'

Actions Handled by Resource Controllers

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

Implicit Controllers

These types of controllers enable developers to define a single route for handling a number of actions throughout the controller. The syntax of using that is by:Example:

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

It is the place your Implicit controller file will get stored: app/Http/Controllers/ImplicitController.php; and can look need to script like:


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 could require inside its constructor. On the other hand, methodology injection permits you to type-hint dependencies for the controller’s action methodology in our Laravel project.

<— Previous Tutorial Next Tutorial —>


Please enter your comment!
Please enter your name here


Related Articles

Concept of Session in Laravel Tutorial

Sessions are used to store details about the user throughout the requests. Laravel supplies various drivers like file, cookie, apc, array, Memcached, Redis, and database to handle session data. By default, file driver is used as a result of it's light-weight....

Laravel Url Generation Tutorial

Our web application revolves around routes and URLs. After all, they're what direct our users to our pages. At the end of the day, serving pages is what any web application should do. Our users may...

Concept of Laravel Views Tutorial

In MVC framework, the letter "V" stands for Views. It separates the application logic and presentation logic. Views are saved in resources/views listing. Generally, the view contains the HTML which might be served by the application.
WP2Social Auto Publish Powered By :