Home » How to create Controller in Laravel?

How to create Controller in Laravel?

In this tutorial, We will learn you how to create controller in laravel 5/6/7 web application. we will show you best step by step examples of make a controller in laravel version 4/5/6/7 or more using cmd command. we will use php artisan make controller command to create controller in laravel web project.

You have to see this article for making controller in laravel using PHP artisan command. you can simply create controller with windows cmd command and terminal with Laravel Crud.

What is Controller in Laravel?

In Laravel Controller handle all request of routes files and write logic for Views and Models. Using Controller you can easily bind models and views logic on it.

You can easily create controller using laravel command.

what the controllers do?

Controllers are meant to package associated handle to all the request handling main logic within a single user define class. In your Laravel web application, they are saved in the app/Http/Controllers’ folders. also I show you The full form of MVC is Great Model View Controller, which act as matching or getting all the passage between the client side Views parts and the main Models.

Read Also:  Woocommerce get price in custom loop

How to create Controller in Laravel using Artisan Command?

Here, we will see how to create controller on laravel. You can simply make a controller by following bellow command:

Creating Controllers

first of all Open your CMD or terminal and type the simple PHP artisan command:

Syntax

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

Example

php artisan make:controller AssumeTestController

And then you can see laravel controller file on bellow path to added source code:

app/Http/Controllers/AssumeTestController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class AssumeTestController extends Controller
{
       
}

You can easily here connect route navigation with MVC Based controller as like bellow:

routes/web.php

Route::get('testpage', '[email protected]');

app/Http/Controllers/AssumeTestController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class AssumeTestController extends Controller
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public function index()
    {
        return view('testpage');
    }
}

You can also simply create controller with invokable controller using bellow command:
php artisan make:controller ListAllUser --invokable

app/Http/Controllers/ListAllUser.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class ListAllUser extends Controller
{
    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function __invoke(Request $request)
    {
          
    }
}

Also You can simply create resource controller. I already written great Article on it. You can see that by clicking here: Learn step by step Resource Controller.

Read Also:  Laravel 5.7 Stripe Payment Gateway Example From Scratch

laravel create controller

The resource route of Laravel allows the classic “CRUD” like as a(GET, POST, PUT and DELETE) all the methods here availble routes for controllers having a basic single line of source code. It can be created fastly using the make: controller command (Artisan command) something like this”

Example

php artisan make:controller SecrateController --resource

The above source code will produce a controller in app/Http/Controllers/ path with file name SecrateController.php which will hold a native method for all available all the methods of resources.

Read Also:  How to get last executed query in PHP Codeigniter?

PHP Laravel Framework developers also have the freedom to register multiple base resource controllers at a time by passing an array to resource native method something such this –

Route::resources([
    'password' => 'SecrateController',
    'dpimg' => 'ProfileController'
]);

Actions Handled by Resource Controllers

Verb URI Action Route Name
GET /members Members list members.index
POST /members/add Add a new member  members.add
GET /members/{member} Get member  members.show
GET /members/{member}/edit Edit member  members.edit
PUT /members/{member} Update member  members.update
DELETE /members/{member} Delete member  members.destroy

I hope you get an idea about laravel register controller.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  How to create model object in controller in laravel?
  2. Read Also:  Laravel 7/6 disable registration example
  3. Read Also:  Laravel JSON Encode Decode Handling
  4. Read Also:  Add Word Count to Single Posts in WordPress
  5. Read Also:  Laravel 6 Deletes method using Eloquent ORM
  6. Read Also:  PHP JQuery Ajax Request POST Data
  7. Read Also:  PHP MySQLi RESTful API with Slim Framework
  8. Read Also:  PHP Time Ago Helper Function
  9. Read Also:  PHP Ajax Form Validation without page refresh
  10. Read Also:  PHP Laravel Paths Example Tutorial From Scratch

Leave a Comment

Your email address will not be published. Required fields are marked *