具有固定参数值的路由的别名 [英] Alias for a route with a fixed parameter value
本文介绍了具有固定参数值的路由的别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这条路线:
Route::get('/MyModel/{id}', 'MyController@show');
方法show()
接受名为id
的参数,我想为/MyModel/1
设置别名,以便可以从/MyCustomURL
访问.
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]);
但是我不断遇到方法show()
缺少必需参数的错误.
But I keep getting missing required argument error for method show()
.
在Laravel中有没有一种干净的方法来实现这一目标?
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.
以下是示例:
Route::get('/alias', 'MyController@show')->defaults('id', 1);
在这种情况下,您不需要在控制器中添加其他方法.
In this case you don't need to add additional method in your controller.
这篇关于具有固定参数值的路由的别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文