带有可选URL $ param(s)的命名路由-Laravel 4 [英] Named routes with optional url $param(s) - Laravel 4
问题描述
我遇到了一个关于L4(可能是symfony2?)路由的怪癖,对于它,我似乎无法在线或在精彩的Code Bright中找到任何资源,而在IRC中却空无一物.
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.
我正在尝试通过控制器使用具有命名路由的可选参数,但是在加载视图时会收到错误消息.
I'm trying to use optional params with a named route through a controller, but receive an error when loading the view.
Route::get('/topic/{topicID?}', array(
'as' => 'topicDetails',
'uses' => 'TopicController@showTopic'
));
控制器:
class TopicController extends BaseController {
public function showTopic($topicID = null)
{
$data['topicID'] = $topicID;
return View::make('topic_view', $data);
}
}
查看
<a href="{{ route('topicDetails') }}">XXX</a>
错误:
Parameter "topicID" for route "topicDetails" must match "[^/]++" ("" given) to generate a corresponding URL.
我假设这没有将null
值传递给$param
,但是我对L4不够熟悉,无法弄清为什么它不起作用,并且我已经用光了所有资源.
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.
任何线索将不胜感激,谢谢!
Any clues would be greatly appreciated Thanks!
推荐答案
此
<a href="{{ route('topicDetails') }}">XXX</a>
应该是
<a href="{{ route('topicDetails', null) }}">XXX</a>
这篇关于带有可选URL $ param(s)的命名路由-Laravel 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!