Laravel | Controller Basics

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.

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:

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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
    <head>
        <title>GfG</title>
        <style>
            h1 {
                color: green;
            }
        </style>
    </head>
    <body>
        <h1>GeeksforGeeks</h1>
        <h3>A computer science portal for geeks</h3>
    </body>
    </html>

    chevron_right

    
    

  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.

Output:

Reference: https://laravel.com/docs/6.x/controllers



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.