[FirstProject \ Http \ Controllers \ UserController]上不存在方法[showpath]

时间:2017-12-22 08:38:38

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

您好我第一次在主题控制器上学习laravel。我需要获得此输出

第一个中间件, 第二个中间件, URI:usercontroller / path, 网址:http://localhost:8000/usercontroller/path, 方法:GET

我的以下代码是:

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

时 我得到了 BadMethodCallException [FirstProject \ Http \ Controllers \ UserController]上不存在方法[showPath]。

有什么问题?

1 个答案:

答案 0 :(得分:1)

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