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.

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


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

routes/web.php

Route::get('testpage', 'AssumeTestController@index');

app/Http/Controllers/AssumeTestController.php


You can also simply create controller with invokable controller using bellow command:

php artisan make:controller ListAllUser --invokable

app/Http/Controllers/ListAllUser.php


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.

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.

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.

Leave a Comment