Q:

NotFoundHttpException in Laravel middleware and controller

I am new to laravel and learning basics from https://www.tutorialspoint.com/laravel/laravel_middleware.htm
After installation I have created RoleMiddleware and TestController.
RoleMiddleware.php
<?php

namespace App\Http\Middleware;

use Closure;

class RoleMiddleware
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next, $role)
{
    echo "Role: ".$role;
    return $next($request);
}
}

and TestController.php
<?php

namespace App\Http\Controllers;

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

class TestController extends Controller
{
//
public function index(){
  echo "<br>Test Controller.";
}
}

and app\http\ routes.php
 Route::get('/role',[
'middleware' => 'Role:editor',
'uses' => 'TestController@index',
 ]);

and in Kernel.php
protected $routeMiddleware = [
  'auth' => \App\Http\Middleware\Authenticate::class,
  'auth.basic' => \Illuminate\Auth\Middleware   \AuthenticateWithBasicAuth::class,
  'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
  'Age' => \App\Http\Middleware\AgeMiddleware::class,
  'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
  'Role' => \App\Http\Middleware\RoleMiddleware::class,
  'terminate' => \App\Http\Middleware\TerminateMiddleware::class,
  ];

After written all these code I have executed this using http://localhost:8000/role but it gives NotFoundHttpException, Please help me to solve this issue..

A:

First of all you are using some older version of Laravel, as your routes are listed in app\http\routes.php file  because in newer versions, it is actually routes\web.php file:
Then you made a mistake in your route:
 Route::get('/role', ['middleware' => 'Role:editor', 'uses' => ]);

http routes must have a callback/closure, whether it is custom function or using a controller... Try with following basic options:
Route::get('/role', function() { return "Working"; });

Also check whether your route is not listed as excepted route
If its working, then play with middlewares!!!

