laravel 4 - 同一控制器功能上的两条路由

时间:2013-08-18 14:43:16

标签: php laravel laravel-4

我想做一些我不知道是否可行的事情。

这是我的routes.php

Route::get('mockups/user/skills/{skill}', 'MockupsController@user');
Route::get('mockups/user/tours/{tour}', 'MockupsController@user');
Route::get('mockups/user', 'MockupsController@user');

和我的MockupsController @用户函数

public function user($skill=null,$tour=null){
    if($tour ==null && $skill != null)
        return View::make('demo/mockups/user/public',array('skill'=>$skill));
    if($tour!=null && $skill ==null)
        return View::make('demo/mockups/user/public',array('tour'=>$tour));
    return View::make('demo/mockups/user/public');

}

如果我收到/mockups/user/tours/tour1网址,则控制器会调用demo/mockups/user/public视图,而不会发送$tour变量。如何使其有效?

修改

public function user($skill=null,$tour=null){
    echo var_dump($skill);
    echo var_dump($tour);
    die();
    if($tour ==null && $skill != null)
        return View::make('demo/mockups/user/public',array('skill'=>$skill));
    if($tour!=null && $skill ==null)
        return View::make('demo/mockups/user/public',array('tour'=>$tour));
    return View::make('demo/mockups/user/public');

}

显示

string(5) "tour1" NULL 

1 个答案:

答案 0 :(得分:2)

我不明白为什么你这么复杂。为什么不做呢

Route::get('mockups/user/skills/{skill}', 'MockupsController@skills');
Route::get('mockups/user/tours/{tour}', 'MockupsController@tours');
Route::get('mockups/user', 'MockupsController@user');

public function skills($skill)
{
    return View::make('demo/mockups/user/public',array('skill'=>$skill));
}

public function tours($tour)
{
    return View::make('demo/mockups/user/public',array('tour'=>$tour));
}

public function user()
{
    return View::make('demo/mockups/user/public');
}