Laravel 4路由与params问题

时间:2014-11-26 05:17:21

标签: php laravel laravel-4

所以我要做的是创建一个显示用户配置文件的链接。我想要的链接是localhost / user / {username},但我用我目前拥有的代码获得的链接是localhost / user?{username}。 这是我的路线代码:

/* Profile Link */
Route::get('user/{username}', array(
'as' => 'profile-user-link'
));

我没有使用控制器。这是我的观看代码:

<li><a href="{{ URL::route('profile-user-link', Auth::user()->username) }}">My Profile</a></li>

有人可以解释为什么我没有得到我想要的链接输出。谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用以下内容:

Route::get(
    'user/{username}',
    array(
        'as' => 'profile-user-link',
        'uses' => 'userController@profile' // <-- You didn't use any handler
    )
);

<强> UserController中:

class UserController extends BaseController {

    public function profile($username)
    {
        // Make sure User model and username field exists
        $user = User::where('username', $username)->first();
        // Do something with $user
    }
}

或者您也可以使用类似的东西(不良做法):

Route::get('user/{username}', function($username) {
    // Make sure User model and username field exists
    $user = User::where('username', $username)->first();
    // Do something with $user
});