What is controller & How to create controller in Laravel?

Posted by

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

 A Controller is that which controls the behavior of a request. It handles the requests coming from the Routes. In Laravel, a controller is in the ‘app/Http/Controllers’ directory. All the controllers, that are to be created, should be in this directory.

We can create a controller using ‘make:controller’ Artisan command.

Syntax

php artisan make:controller UserController

You can specify any name in place of ‘User’, but according to the naming convention of Laravel, you have to specify the word ‘Controller’ at the end for any name you specify.

Example

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class GfGController extends Controller
{
    public function index() {
        return view('gfg');
    }
}
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x