dongyue8640
2015-07-26 12:14
浏览 89
已采纳

控制器方法未定义错误 - Laravel 5.1

I am trying to display the currently logged in username, as a link to the user info, in my main navigation. I can get the name to display, so I am pulling the info from the db, and passing it to the view OK. But, when I try to make it a link, I get the method not defined error.

Here is how I pass the user info to the navigation (as the var $userInfo):

public function index()
{
    $Clients = \Auth::user()->clients()->get();
    $userInfo = \Auth::user();
    return view('clients.index', compact('Clients', 'userInfo'));
}

Here is the relevant bit from my navigation:

<ul class="nav navbar-nav">
  <li>{!! link_to_action('AuthController@show', $userInfo->username, [$userInfo->id]) !!}</li>
</ul>

The method from my controller:

    protected function show($id)
{
    $userInfo = User::findOrFail($id);
    return view('users.show', compact('userInfo'));
}

And, the route definition:

// User Display routes
Route::get('auth/{id}', 'Auth\AuthController@show');

Here is the error I get:

Action App\Http\Controllers\AuthController@show not defined. 

Can anyone tell me what I am missing?

图片转代码服务由CSDN问答提供 功能建议

我试图在主导航中显示当前登录的用户名,作为用户信息的链接。 我可以显示名称,所以我从数据库中提取信息,并将其传递给视图确定。 但是,当我尝试将其设为链接时,我得到方法未定义错误。

以下是我将用户信息传递给导航的方式(作为var $ userInfo):< / p>

  public function index()
 {
 $ Clients = \ Auth :: user() - &gt; clients() - &gt; get(); 
 $ userInfo  = \ Auth :: user(); 
返回视图('clients.index',compact('Clients','userInfo')); 
} 
   
 
 <  p>以下是我导航中的相关位: 
 
 
 &lt; ul class =“nav navbar-nav”&gt; 
&lt; li&gt; {!!  link_to_action('AuthController @ show',$ userInfo-&gt; username,[$ userInfo-&gt; id])!!}&lt; / li&gt; 
&lt; / ul&gt; 
   
  
 

来自我的控制器的方法:

 受保护的函数show($ id)
 {
 $ userInfo = User :: findOrFail($ id); \  n return view('users.show',compact('userInfo')); 
} 
   
 
 

并且,路由定义:

  //用户显示路线
Route :: get('auth / {id}','Auth \ AuthController @ show'); 
   
 
  

以下是我得到的错误:

 操作App \ Http \ Controllers \ AuthController @ show未定义。  
   
 
 

谁能告诉我我缺少的东西?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doufang1954 2015-07-26 12:23
    已采纳

    First, you need to make your AuthController::show() method public:

    public function show($id)
    {
      $userInfo = User::findOrFail($id);
      return view('users.show', compact('userInfo'));
    }
    

    Second, as your controllere is in App\Http\Controllers\Auth namespace, you need to use the **Auth** prefix in the view:

    <ul class="nav navbar-nav">
      <li>{!! link_to_action('Auth\AuthController@show', $userInfo->username, [$userInfo->id]) !!}</li>
    </ul>
    
    点赞 打赏 评论

相关推荐 更多相似问题