带有可选 url $param(s) 的命名路由 - Laravel 4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18029120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Named routes with optional url $param(s) - Laravel 4
提问by jakeharris
I've ran into a bit of a quirk with L4 (possibly symfony2?) routing for which I can't seem to find any resources online or in the wonderful Code Bright, and came up empty in IRC.
我在 L4(可能是 symfony2?)路由方面遇到了一些怪癖,我似乎无法在线或在精彩的 Code Bright 中找到任何资源,而在 IRC 中却是空的。
I'm trying to use optional params with a named route through a controller, but receive an error when loading the view.
我试图通过控制器使用带有命名路由的可选参数,但在加载视图时收到错误。
Route:
路线:
Route::get('/topic/{topicID?}', array(
'as' => 'topicDetails',
'uses' => 'TopicController@showTopic'
));
Controller:
控制器:
class TopicController extends BaseController {
public function showTopic($topicID = null)
{
$data['topicID'] = $topicID;
return View::make('topic_view', $data);
}
}
View
看法
<a href="{{ route('topicDetails') }}">XXX</a>
Error:
错误:
Parameter "topicID" for route "topicDetails" must match "[^/]++" ("" given) to generate a corresponding URL.
I'm assuming this isn't passing the null
value to the $param
but I'm not familiar enough with L4 to figure out why it isn't working, and I've exhausted all my resources.
我假设这不会将null
值传递给 ,$param
但我对 L4 不够熟悉,无法弄清楚它为什么不起作用,而且我已经用尽了所有资源。
Any clues would be greatly appreciated Thanks!
任何线索将不胜感激谢谢!
回答by Laurence
this
这个
<a href="{{ route('topicDetails') }}">XXX</a>
should be
应该
<a href="{{ route('topicDetails', null) }}">XXX</a>