【问题标题】:Laravel 4 route NOT passing in a usernameLaravel 4路由未传入用户名
【发布时间】:2015-03-07 02:39:25
【问题描述】:

嗨,我已经尝试解决这个问题几个小时了,研究了 Stack Overflow 和许多其他网站,但没有运气。

//-------Route
Route::get('/profile/{firstName}', array(
    'as' => 'userProfile',
    'uses' => 'ProfileController@user'
));

//--------Controller 

class ProfileController extends BaseController {

    public function user($firstName) {

        $user = User::where('firstName', '=', $firstName);

        if($user->count()) {
            //First record return from the query
            $user = $user->first();

            return View::make('profile.user')
                ->with('user', $user);
        }

        return "sorry, 404";
        //return App::abort(404);
    } 
}

最后链接:

 <li><a href="{{URL::route('userProfile') }}"> My Profile </a></li>

------------------

*如果我手动将用户名放入 URL,它可以工作,但是当我点击链接时,我得到 localhost:8000/profile/{firstName} 并带有“抱歉,404”

谢谢你的帮助!!!

【问题讨论】:

    标签: model-view-controller laravel-4 controller routing url-routing


    【解决方案1】:

    您的路由userProfile 需要参数firstName,因此您必须在生成url 时将其传入:

    {{ URL::route('userProfile', Auth::user()->firstName) }}
    

    (您如何在视图中获得firstName 并不重要,我只使用了Auth::user,因为我认为这可能是您的用例)

    【讨论】:

      猜你喜欢
      • 2014-04-26
      • 2014-02-05
      • 1970-01-01
      • 2014-08-17
      • 2014-06-25
      • 2013-01-15
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多