简体   繁体   English

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

[英]Controller Method not defined error - 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. 我可以显示名称,因此我正在从数据库中提取信息,并将其传递给视图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): 这是我将用户信息传递给导航的方式(如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? 谁能告诉我我想念的东西吗?

First, you need to make your AuthController::show() method public : 首先,您需要将AuthController :: show()方法设为 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: 其次,由于您的控制器位于App \\ Http \\ Controllers \\ Auth命名空间中,因此您需要在视图中使用** Auth **前缀:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM