LeCintas LeCintas - 1 month ago 8
PHP Question

Wildcard to route to a controller

I am absolutely novice with Laravel and I am currently learning the routes feature of the Framework.

I want to know if it is possible to do like we can do with ASP.NET that is to redirect to a Controller written in the URL.

Actually, to do something like:

<?php

Route::get('/{CustomController}/{Action}', function ($controller, $action) {
return controller($controller, $action);
});


Where
CustomController
is the controller which will run the request to the view
Action


With ASP.NET Core, it is something like:

routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}");
});

Answer

Yes, with routes in laravel the format is this:

Route::get('/custom-controller', 'CustomController@index');

If you are using the standard method names in your controller such as index(), store() etc

you can use resource instead and it will automatically build up all your routes:

Route::resource('/my-route', 'CustomController');

If you then do php artisan route:list you will see laravel has automatically added all the standard routes for your controller.