Route List And Required Parameter In Laravel

Hii there ! Today we are going to explore how to get all the routes list with command line and how to declare routes for required parameters.

1.Route List

route:list command  is used to get all routes defined by application.

To do  so in your project terminal or in cmd simply type

php artisan route:list

but what if we wants to get all middleware that are being assigned to that particular route with route list.

Very simple, In laravel by default route middleware are not displayed with route list in laravel.

Add -v at the end of route:list

php artisan route:list -v

This list that comes as result of executing one of the above command  may also come with routes that are being defined by third party packages.

To avoid or to stop displaying third party declared routes with the route:list we can use  –except-vendor when executing the command.

php artisan route:list --except-vendor

2. Required Parameters In Laravel.

Sometime we need to capture the segment of URl within  our route. That is where the role of route parameters comes into picture.

For Example,  Let’s sat that we wants to capture the message from the URL.

We can do that by defining routing parameters.

Route::get('/user/{message}', function ($message) {
    return 'User '.$message;
});

We can define more than one route or as many as per our requirement. No issue with that.

Route::get('/posts/{post}/comments/{comment}', function ($postId, $commentId) {
    //your code goes here
});

This parameters are always declared within {} braces.

It could be  characters, underscore, that both are acceptable as parameter names.

this parameters are injected into route callbacks as shown above. here name of the route callback do not matter.

That is all for today.

Thanks For Read.

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories