Laravel 5.0 屏蔽路由

Laravel 5.0 masking routes

我是 Laravel 世界的新手(使用 5.0),我正在学习如何路由。 我有这条路线

Route::get('users/{id}', 'UserController@showProfile');

UserController

public function showProfile($id)
    {
         return view('user.profile', ['user' => User::findOrFail($id)]);
    }

一切正常,生成的 url 例如localhost:8000/users/1.

是否可以屏蔽这条路线,而是使用 localhost:8000/users/profile 之类的东西,在幕后进行查询? 谢谢大家

最简单的方法就是简单地拉入经过身份验证的用户:

Route::get('users/profile', 'UserController@showProfile');



public function showProfile()
{
    return view('user.profile', ['user' => Auth::user()]);
}

您可以检查 $id 类型。

public function showProfile($id) {
    if(is_numeric($id)) {
        return view('user.profile', ['user' => User::findOrFail($id)]);
    } else {
        // Profile page.
        return view('user.profile_page');
    }
}