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.
Php artisan make: Controller MyController
MyController.php file will be created whose default code is as below.
<?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.
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.
<?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:
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.
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
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:
Route::get('\user','User\UsersController@show');
Single Action Controllers
If we want to use the single method in a controller, then we can use the single __invoke() method on the controller.
When we create the controller by using the command php artisan:make controller PostController then the structure of the PostController file would be:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
//
}
Now, we add the code of __invoke() function in a PostController class:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
//
public function __invoke($id)
{
return "id is : ". $id;
}
}
In the end, we add the code in the web.php file, which is responsible for handling the actions.
route::get('/post/{id}','PostController');
The above code hits the __invoke() method of a PostController class. This concludes that we do not need to write the @invoke method for accessing the single action controllers.