I have this route:
Route::get('/MyModel/{id}', 'MyController@show');
The method show()
accepts a parameter called id
and I want to setup an alias for /MyModel/1
so it's accesible from /MyCustomURL
.
I already tried a few combinations, like:
Route::get('/MyCustomURL', ['uses' => 'MyController@show', 'id' => 1]);
But I keep getting missing required argument error for method show()
.
Is there a clean way to achieve this in Laravel?
In Laravel 5.4 (or maybe earlier) you can use
defaults
function in your routes file.Here is example:
In this case you don't need to add additional method in your controller.
not
Good Luck!
In same controller (in your case
MyController
?) you should create one new method:and now you can define your aliased route like so:
define your route like this: you can use "as" to give your route any name that you need.
now if you want to access this route, you can generate url for it, based on its name like this: