Named routes with optional url $param(s) - Laravel 4

人盡茶涼 提交于 2019-12-21 05:17:11

问题


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:

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.

Any clues would be greatly appreciated Thanks!


回答1:


this

<a href="{{ route('topicDetails') }}">XXX</a>

should be

<a href="{{ route('topicDetails', null) }}">XXX</a>


来源:https://stackoverflow.com/questions/18029120/named-routes-with-optional-url-params-laravel-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!