Camilo Camilo - 3 years ago 158
PHP Question

Alias for a route with a fixed parameter value

I have this route:

Route::get('/MyModel/{id}', 'MyModel@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' => 'MyModel@show', 'id' => 1]);


But I keep getting missing required argument error for method
show()
.

Is there a clean way to achieve this in Laravel?

Answer Source

In same controller (in your case MyModel ?) you should create one new method:

public function showAliased()
{
   return $this->show(1);
}

and now you can define your aliased route like so:

Route::get('/MyCustomURL', 'MyModel@showAliased');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download