How to Create Controller in Laravel

By | April 21, 2021

What is Controller In Laravel-: Controller is a center unit of any MVC framework. We know that the laravel work on the MVC framework. Controller is the main part of Laravel MVC. In other words we can say that the controller is a class that store request logic in a single class. In a class we can create many function and call them.

laravel controller

When we create a project or install laravel project then a controller has been created in the app\http\controller. We can make another controller using the cmd command line.

How to Create a Controller in Laravel

In the laravel we can create all the controller using the command line. For creating a controller in laravel we open the project using the cmd command line then give this command

php artisan make:controller controller_name

Here we give the controller_name and press the enter then a controller has been created in our project. we can see this controller in our project. First open app folder now open http folder. in the http folder has a controller folder. All the controller are show here.

Make a Function in controller -: In the controller we can create function that will use in our project. we can create a function as like

<php ?
Namespace App\http\controllers;

use illuminate\http\Request;
class controller_name extend controller()
{
    Public function function_name()
    {
      // Here we will write the logic that we use in our program.
    }
}

How to Call Controller From Routing

What is Routing -: When we install laravel then a routing folder is install. There are four type file has been installed in routes folder. In this type we has a web.php file. Routing is a mapping of specific url laravel page.

Call a controller from Routing-: First we create a function in controller then we call it. For calling a controller open the web.php file in the route folder.

<php ?

use illuminate\support\Facades\Route;
use App\http\controllers\controller_name;


Route::get('page_url','Controller_name@function_name'); // for seeing the data
Route::post('page_url','Controller_name@function_name'); // for sending data we use post

if we are using laravel 8 then we use this method for declare controller
Route::get('page_url','[Controller_name::Class','function_name']);
Route::post('page_url','[Controller_name::Class','function_name']); 

Example of Controller -: Here we will create a users controller in laravel. Here we will make a page in view and give the name of the page index.blade.php. Here we will create a function name is users_data Now we write the code in controller as like

<php ?
Namespace App\http\controllers;

use illuminate\http\Request;
class users extend controller()
{
    Public function users_data()  // first function
    {
      echo "Welcome in the controller"; // for printing data we use echo
    }

    public function student_data($data) // this is the another function
   {
    return  ['name'=>"Netnic",'Domain_age'=>4];
   }
}

Now we call this function in routes . Here we have created multiple function we can call to different url

<php ?

use illuminate\support\Facades\Route;
use App\http\controllers\users;


Route::get('index','users@user_data'); // for seeing the data
Route::post('index','users@user_data'); // for sending data we use post

Now we run this program we will see the result on index url

Welcome in the controller

Leave a Reply

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