0

我有一个哨兵 laravel 4 设置。

此刻,对于个人资料视图,我应该像这样链接

http://localhost/ffdd/public/3/show

其中 3 是用户 ID

我正在使用以下路线

Route::get('/{profile}/show', array('as' => 'profile.show', 'uses' => 'ProfilesController@show'));

我需要相同的链接结果

http://localhost/ffdd/public/3

我应该改变什么路线。

并且对于将来,我希望这个用户 id 位置为 username 。

编辑 2

我目前的路线结构是这样的。

Route::get('/{userId?}', array('as' => 'profile', 'uses' => 'ProfilesController@index')); 
Route::post('/{userId?}', array('as' => 'profile-koko', 'uses' => 'Controllers\Account\ProfileController@postIndex'));

Route::get('/{profile}/show', array('as' => 'profile.show', 'uses' => 'ProfilesController@show'));

当profilescontroller 的索引收到路由请求时,它只是将其转发给show 方法。

 // User is logged in
                $user = Sentry::getUser();
                $user = $user->id;

                //return View::make('profiles.index');
                return Redirect::action('ProfilesController@show', array('userId'=>$user));
            }

当我使用这个

Route::get('{profile}',array(
        'as'    =>  'test2',
        'uses'  =>  'ProfilesController@show',
    ));

正如我的朋友 itachi 在他的回复中所说!

我收到这个错误!

Some mandatory parameters are missing ("profile") to generate a URL for route "profile.show".

编辑 3

<?php

# Profile

Route::get('/{userId?}', array('as' => 'profile', 'uses' => 'ProfilesController@index')); 
Route::post('/{userId?}', array('as' => 'profile-koko', 'uses' => 'Controllers\Account\ProfileController@postIndex'));

Route::resource('profile', 'ProfilesController@show');


Route::get('{profile}/show',array(
        'as'    =>  'profile.show',
        'uses'  =>  'ProfilesController@show',
    ));

    Route::get('{profile}',array(
        'as'    =>  'test2',
        'uses'  =>  'ProfilesController@show',
    ));
4

1 回答 1

2

我看不到为同一操作定义两条路线的任何用途。但是,如果你想要它,它就在这里。

只需创建两条指向同一动作的路线。例如

    Route::get('{profile}/show',array(
        'as'    =>  'test1',
        'uses'  =>  'ProfileController@show',
    ));

    Route::get('{profile}',array(
        'as'    =>  'test2',
        'uses'  =>  'ProfileController@show',
    ));

现在,您可以同时访问路线http://localhost/ffdd/public/3/showhttp://localhost/ffdd/public/3

但是这里有一个问题。{profile}参数可以匹配任何东西。

例如

http://localhost/ffdd/public/3/show [将按预期调用 ProfileController@show]

http://localhost/ffdd/public/asdf/show [还将调用 ProfileController@show这不是本意的!]

为了避免这个问题,你有两种方法。

  1. 在文件的最后声明这两条路由,以便其他路由优先。

  2. 由于{profile}必须是一个 id [现在],让我们通过声明以下内容来对其进行约束,

Route::pattern('profile', '[0-9]+');

现在,{profile}将只匹配数字。

所以整个代码变成了

    Route::pattern('profile', '[0-9]+');

    Route::get('{profile}/show',array(
        'as'    =>  'test1',
        'uses'  =>  'ProfileController@show',
    ));

    Route::get('{profile}',array(
        'as'    =>  'test2',
        'uses'  =>  'ProfileController@show',
    ));

这里不会有任何冲突,因为{profile}必须是一个数字才能唤起ProfileController@show动作。

放置约束的替代方法

还有一种替代方法可以使用where.

例如

Route::get('{profile}/show',array(
    'as'    =>  'test1',
    'uses'  =>  'ProfileController@show',
))->where('profile', '[0-9]+');

但是如果你走这条路,你需要把它放在where你使用的每条路线中{profile}

于 2013-11-30T06:55:12.350 回答