0
votes

Hello i am learning laravel for first time on topic controllers.I need to get this output

First Middleware, Second Middleware, URI: usercontroller/path, URL: http://localhost:8000/usercontroller/path, Method: GET

My Following codes are:

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;
   }
}

Routes/web.php

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

But When im running http://localhost:8000/usercontroller/path

I m getting BadMethodCallException Method [showPath] does not exist on [FirstProject\Http\Controllers\UserController].

What is the problem?

1

1 Answers

1
votes

It is quite obvious, isn't it ? This method is defined in SecondUserController but not in UserController and in routes you use 'UserController@showPath'