使用路由的laravel中配置文件的Url结构


Url structure for profile in laravel using routes

我有一个哨兵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',
    ));

我不认为为同一操作定义两个路由有任何用处。但是,如果你想要的话,它还是来了。

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

    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',
    ));

这里不会有任何冲突,因为CCD_ 7必须是数字才能调用CCD_。

设置约束的替代方法

还有一种使用where放置约束的替代方法。

例如

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

但如果你走这条路,你需要把这个where放在你使用{profile}的每一条路线上。