Okay so I’m kinda new to laravel and I’m trying to use Spatie for roles and permission but this error happened, even though I’ve written the class name in kernel.php here :
<?php
namespace AppHttp;
use IlluminateFoundationHttpKernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array<int, class-string|string>
*/
protected $middleware = [
// AppHttpMiddlewareTrustHosts::class,
AppHttpMiddlewareTrustProxies::class,
IlluminateHttpMiddlewareHandleCors::class,
AppHttpMiddlewarePreventRequestsDuringMaintenance::class,
IlluminateFoundationHttpMiddlewareValidatePostSize::class,
AppHttpMiddlewareTrimStrings::class,
IlluminateFoundationHttpMiddlewareConvertEmptyStringsToNull::class,
];
/**
* The application's route middleware groups.
*
* @var array<string, array<int, class-string|string>>
*/
protected $middlewareGroups = [
'web' => [
AppHttpMiddlewareEncryptCookies::class,
IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
IlluminateSessionMiddlewareStartSession::class,
IlluminateViewMiddlewareShareErrorsFromSession::class,
AppHttpMiddlewareVerifyCsrfToken::class,
IlluminateRoutingMiddlewareSubstituteBindings::class,
],
'api' => [
// LaravelSanctumHttpMiddlewareEnsureFrontendRequestsAreStateful::class,
IlluminateRoutingMiddlewareThrottleRequests::class.':api',
IlluminateRoutingMiddlewareSubstituteBindings::class,
],
];
/**
* The application's middleware aliases.
*
* Aliases may be used instead of class names to conveniently assign middleware to routes and groups.
*
* @var array<string, class-string|string>
*/
protected $middlewareAliases = [
'auth' => AppHttpMiddlewareAuthenticate::class,
'auth.basic' => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class,
'auth.session' => IlluminateSessionMiddlewareAuthenticateSession::class,
'cache.headers' => IlluminateHttpMiddlewareSetCacheHeaders::class,
'can' => IlluminateAuthMiddlewareAuthorize::class,
'guest' => AppHttpMiddlewareRedirectIfAuthenticated::class,
'password.confirm' => IlluminateAuthMiddlewareRequirePassword::class,
'permission' => SpatiePermissionMiddlewaresPermissionMiddleware::class,
'precognitive' => IlluminateFoundationHttpMiddlewareHandlePrecognitiveRequests::class,
**'role' => SpatiePermissionMiddlewaresRoleMiddleware::class,**
'role_or_permission' => SpatiePermissionMiddlewaresRoleOrPermissionMiddleware::class,
'signed' => AppHttpMiddlewareValidateSignature::class,
'throttle' => IlluminateRoutingMiddlewareThrottleRequests::class,
'verified' => IlluminateAuthMiddlewareEnsureEmailIsVerified::class,
];
}
Did I miss something or write something wrong?
(By the way, I use Laravel v10.30.1 if that helps)
I try to access a view using this routing :
Route::middleware('role:super-admin')->get('/dashboard', [AdminController::class, 'index'])->name('admin');
The vendor frame thingy tells me this :
publicindex.php : 51 require_once
Below is the line the error happened :
$app = require_once __DIR__.'/../bootstrap/app.php';
$kernel = $app->make(Kernel::class);
**$response = $kernel->handle(**
$request = Request::capture()
)->send();
2
Answers
Try using
Middleware
instead ofMiddlewares
https://spatie.be/docs/laravel-permission/v6/basic-usage/middleware#content-package-middleware
use Middlewares,
"role" => SpatiePermissionMiddlewaresRoleMiddleware::class
Inside Laravel-permission, in the spatie folder, there is a folder called Middlwares. There’s a file called RoleMiddleware.php in there.
What’s wrong is that you inserted the ** symbol both before and after this line.
‘role’ => SpatiePermissionMiddlewaresRoleMiddleware::class,.
https://spatie.be/docs/laravel-permission/v6/basic-usage/middleware#content-package-middleware.
there is slight changes in this link, In this Middlware is use but when we install the package using composer it will load the folder with name Middleware.