Skip to content
Related Articles
Open in App
Not now

Related Articles

Laravel | Controller Basics

Improve Article
Save Article
  • Last Updated : 30 Dec, 2019
Improve Article
Save Article

Laravel is an MVC based PHP framework. In MVC architecture, ‘C‘ stands for ‘Controller‘. 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.


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.


  1. Let’s create a controller by running the following command:

    php artisan make:controller GfGController

    This will create a file in ‘app/Http/Controllers’ directory with the name ‘GfGController.php’. A controller class is also created in this file with the same name.

  2. Now that we have a controller created, lets define a public function with the name ‘index’ and specify our view file name (we will create it in the next step).

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

  3. Now we have to create and write the code for our view file that we specified in our ‘GfGController.php’. We will create a ‘gfg.blade.php’ file in ‘resources/views’ directory.

    <!DOCTYPE html>
            h1 {
                color: green;
        <h3>A computer science portal for geeks</h3>

  4. Now the last thing to do is to write the route in the ‘web.php’ file in ‘routes’ directory.

    Route::get('gfg', 'GfGController@index');

    Here, we have define the route ‘gfg’ in the first parameter (you can specify anything according to your need), and then the name of the controller we created in the previous step. Also, it important that at the end we specify the function name that we have defined in the controller class, separated by an ‘@’ symbol in between.
    Note: To get the output, we have to write ‘/gfg’ at the end of the URL.



My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!