【问题标题】:method [showpath] does not exist on [FirstProject\Http\Controllers\UserController][FirstProject\Http\Controllers\UserController] 上不存在方法 [showpath]
【发布时间】:2018-06-04 22:00:51
【问题描述】:

你好,我是第一次在主题控制器上学习 laravel。我需要得到这个输出

第一个中间件, 第二个中间件, URI:用户控制器/路径, 网址:http://localhost:8000/usercontroller/path, 方法:获取

我的以下代码是:

UserControler.php

namespace FirstProject\Http\Controllers;

use Illuminate\Http\Request;
use FirstProject\Http\Requests;
use FirstProject\Http\Controllers\Controller;

class UserController extends Controller
{
    public function _construct(){
        $this->middleware('auth');
    }    
}

FirstMiddleware.php

namespace FirstProject\Http\Middleware;

use Closure;

class FirstMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        echo "<br>First Middleware";
        return $next($request);
    }
}

SecondMiddleware.php

namespace FirstProject\Http\Middleware;

use Closure;

class SecondMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        echo "<br>Second Middleware";
        return $next($request);
    }
}

SecondUserController.php

namespace FirstProject\Http\Controllers;

use Illuminate\Http\Request;
use FirstProject\Http\Requests;
use FirstProject\Http\Controllers\Controller;

class SecondUserController extends Controller
{
    public function __construct(){
      $this->middleware('Second');
   }
   public function showPath(Request $request){
      $uri = $request->path();
      echo '<br>URI: '.$uri;

      $url = $request->url();
      echo '<br>';

      echo 'URL: '.$url;
      $method = $request->method();
      echo '<br>';

      echo 'Method: '.$method;
   }
}

路由/web.php

Route::get('/usercontroller/path',[
   'middleware' => 'First',
   'uses' => 'UserController@showPath'
]);

但是当我运行http://localhost:8000/usercontroller/path

我得到 坏方法调用异常 [FirstProject\Http\Controllers\UserController] 上不存在方法 [showPath]。

有什么问题?

【问题讨论】:

    标签: laravel laravel-5 laravel-routing controllers laravel-middleware


    【解决方案1】:

    这很明显,不是吗?此方法在SecondUserController 中定义,但不在UserController 中定义,并且在您使用'UserController@showPath' 的路由中定义

    【讨论】:

    • 是的,但是在教程中,它被告知要创建控制器 UserController 但它已经存在,这就是为什么我必须创建不同的控制器名称。
    • 还有其他帮助吗?
    • 因此,当您更改控制器名称时,您还应该更改 web.php 文件中的控制器名称。现在您在此文件中引用 UserController 中不存在的 showPath 方法,这就是您收到错误的原因。
    猜你喜欢
    • 2020-05-08
    • 2020-10-15
    • 2020-09-01
    • 2016-09-16
    • 1970-01-01
    • 2019-09-30
    • 2018-12-14
    • 2018-11-05
    • 2018-05-25
    相关资源
    最近更新 更多