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

控制器方法未定义错误 - 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?

  • 写回答

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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 这个公式写进SIMULINK中的function模块的代码中应该是什么样的
  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题