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.
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.
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).
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.
>A computer science portal for geeks</
Now the last thing to do is to write the route in the ‘web.php’ file in ‘routes’ directory.
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.
- Laravel | Migration Basics
- Laravel | View Basics
- Laravel | Routing Basics
- Laravel | Eloquent Model Basics
- Laravel | Route::resource vs Route::controller
- Laravel | Artisan Commands to know in Laravel
- AngularJS | ng-controller Directive
- HTML | DOM Audio controller Property
- How to insert HTML into view from AngularJS controller?
- How to execute AngularJS controller function on page load ?
- Model-View-Controller(MVC) architecture for Node applications
- Laravel | Front-end Scaffolding
- Laravel | Directory Structure
- Laravel | Validation Rules
- Laravel | Installation and Configuration
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.