user2226181 user2226181 - 1 month ago 18
PHP Question

Class Illuminate\Routing\Middleware\Rolemiddleware does not exist

I am using laravel , when I am trying to inject middleware in router getting some error

my router code

<?php

Route::get('role',[
'middleware' => 'Role:editor',
'uses' => 'TestController@index',
])->middleware('role');

?>


kernal.php

protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'Age' => \App\Http\Middlware\AgeMiddleware::class,
'Role' => \App\Http\Middlware\RoleMiddleware::class,
];


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


?>

Here is my TestController code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;


class Firstcontroller extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
echo '<br>First Controller';
}

?>


I am getting an error: Class Illuminate\Routing\Middleware\Rolemiddleware does not exist. I am new to Laravel and using Laravel 5.3. Can you please tell me why it is giving me an error

Answer

In kernal.php you are doing spelling mistake in word Middlware

change this

'Age' => \App\Http\Middlware\AgeMiddleware::class,
  'Role' => \App\Http\Middlware\RoleMiddleware::class,

to

'Age' => \App\Http\Middleware\AgeMiddleware::class,
'Role' => \App\Http\Middleware\RoleMiddleware::class,