Updated July 10, 2023
Introduction to Laravel Route Controller
These controllers let you create your controller classes using methods that are used for handling various requests. It would be a lot easier if we understand the concept of laravel route controller with the help of an example. We will go step by step in order to get a good understanding of Routing Controllers. So here we go with the steps:
How to Create Laravel Route Controller?
Here are some of the steps for creating laravel routing controllers which are explained below:
Step 1: The very first step would be to create a controller. If you are not familiar with creating a controller, then go through the below points of creating a controller otherwise move directly to step 2 for Routing Controllers.
Use the below artisan command to create the controller.
Code:
Php artisan make: Controller MyController
MyController.php file will be created whose default code is as below.
Code:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class MyController extends Controller
{
public function show($id)
{
//
}
}
Step 2: Now you have to write this below route in web.php file.
Code:
Route::get('/post','MyController@show');
Here the first parameter is URL which you want access to, and MyController is pretty obviously our controller name. The ‘show’ as you can see in MyController.php file, is the method. So, @show here indicates that the show() method would be called when we hit the URL ‘/post’.
Step 3: You can now add coding lines as shown below.
Code:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class MyController extends Controller
{
/**
*Display resource
*
*/
public function show($id)
{
//
}
/**
*Display resource listing
*
*/
public function index()
{
//
}
/**
*Editing resource
*
*/
public function edit($id)
{
//
}
}
Step 4: Now it’s time to hit the URL. You will get a specified output after entering the URL. Hopefully, we have covered enough insights of controllers that you will be able to access your Controller now. Let’s move ahead now on how we can also pass data to our controller class. Again, it would be much easier if we learn passing data through parameters to our controller with the help of an example.
Data Passing to Controller
1. Write this below route in web.php file:
Code:
Route::get('/post/{id}','MyController@show');
The only difference in defining this route is that this line of code also contains the parameter ‘id’ with the URL.
2. Modify file ‘MyController.php’ as shown below.
Code:
public function show($id)
{
return "ID is :".$id;
}
Here the only difference in the method show() is that we have modified it by passing the parameter ‘id’ in the show() method.
3. Again, let’s hit the URL in our browser. You will be getting output depending on the parameter.
Namespaces
Here is a piece of information about namespaces:
While defining the method Route:: get() for our controller class, there’s no need to mention the full namespace for controller since ‘RouteServiceProvider’ loads almost all of your route files in a route group which has namespace contained in it. You simply need to specify that portion of the name that will come after App/Http/Controllers.
For example, If the controller class’s full path is App/Http/Controllers/User/UsersController, then there’s no need to mention the full namespace. You can simply define the route as follows:
Code:
Route::get('\user','User\UsersController@show');
Single Action Controller
If you wish to define the single-action controller, you will be able to do so by placing a single method ‘__invoke’ on your controller. It is said PHP has some amazingly magical functions and ‘__invoke’ function makes it up to that magic list. With the help of this magic ‘__invoke’ function, you can define class using just one PHP function ‘__invoke’ within it, and at any time when the object will be called it will in turn directly call method ‘__invoke’ without even having manually called $obj->randomFunction().
Now the question arises, where can you use it? Fair enough, it’s our psychology to ask this question every time we encounter any new functionality. As an example, this can be used while implementing SRP (Single Responsibility Principle). Like we made a controller above using the artisan command, similarly using php artisan: make controller MyController, the coding of MyController file will look like as shown below:
Code:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class MyController extends Controller
{
//
}
We are good to add lines of code for the __invoke() method in MyController class. Here is an example of how the class will look like after adding code.
Code:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class MyController extends Controller
{
public function __invoke($id)
{
return “ID is :”.$id;
}
}
Now, web.php file needs to be modified as it is solely responsible for all the action handling. The line of code will look something like this:
Code:
Route::get('/post/{id}','MyController');
This line of code will hit the function ‘__invoke()’ of our MyController class. It can be concluded from here that you don’t necessarily have to write @invoke function in order to access single-action controllers.
Note: In case you don’t specify any action, i.e., you don’t write the method __invoke(), It will throw an Invalid Route Action error, i.e., UnexpectedValueExpression.
Insights for Creating Laravel Routes
Every route in Laravel is defined in route files. These route files can be found in the sub-directory of routes. There is no need to manually load these files as they automatically get loaded by Laravel’s framework. The route file of your application gets stored in file ‘app/Http/routes.php’. General routing for any request in Laravel looks like:
Code:
Route::get ('/', anyFunction() {
return 'This is Index';
});
Code:
Route::post ('user/dashboard', anyFunction() {
return 'This is Dashboard';
});
Mechanism of Routing
The mechanism of routing can be understood with three steps that follow:
- Firstly, create a root URL and run it.
- Make sure your URL matches with the methods in root.php file, only then all related functions will be executed.
- The function first invokes template files and then function view() gets called with a filename in resources/views/.
Conclusion – Laravel Route Controller
Hopefully, so far we have learned a fair number of Laravel Route Controller concepts which will enable you to create your very own Laravel routing controller for your application, which will be secure and powerful at the same time.
Recommended Articles
This is a guide to Laravel Route Controller. Here we discuss the data passing to controller, namespaces, single action controller and various steps to create a Laravel. You can also go through our other suggested articles to learn more –