1
votes

I have recently set up some new middleware as part of a permissions system I am integrating.

I have previously setup custom middleware without issue and believe I have followed the same process, but this time I am getting the following error for every controller I have added my new middleware to:

ReflectionException in Container.php line 734: Class App\Http\App\Http\Middleware\HasPermissionPermissions does not exist

I can see why it is getting an error, as that path does not exist - but I cannot understand where it is getting that path from...

Here is my middleware:

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;

class HasPermissionPermissions
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Auth::user()->hasPermissionTo('permissions'))
        {
            return $next($request);
        }
        else
        {
            flash()->error('You are not authorised to access this route.');
            return redirect('/');
        }
    }
}

My Kernel.php

/**
 * The application's route middleware.
 *
 * @var array
 */
protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'application.status' => \App\Http\Middleware\ApplicationCheck::class,
    'email.confirmation.check' => \App\Http\Middleware\CheckIfConfirmed::class,
    'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'hasPermissionAnnouncements' => \App\Http\Middleware\HasPermissionAnnouncements::class,
    'hasPermissionGlobalLog' => \App\Http\Middleware\HasPermissionGlobalLog::class,
    'hasPermissionLaravelLog' => \App\Http\Middleware\HasPermissionLaravelLog::class,
    'hasPermissionPermissions' => \App\Http\Middleware\HasPermissionPermissions::class,
    'hasPermissionUsers' => \App\Http\Middleware\HasPermissionUsers::class,
];

And finally, the controller I am setting this up in:

class PermissionsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('hasPermissionPermissions');
        $this->middleware('email.confirmation.check');
    }

If I remove $this->middleware('hasPermissionPermissions'); it works perfectly, but I can't see how I have set it up any different!

Can anyone see where I am going wrong?

1
composer dump-autoload would doCerlin
@CerlinBoss I've tried that and composer dump-autoload -oJames
Try changing the middleware key from hasPermissionPermissions to hasPermission.permissions, and see if that works.patricus
sorry did see the namespace properly. This error will come when you put relative namespaceCerlin

1 Answers

0
votes

This is a good question, this might be a simple Idea and I think you have done this, but are you sure the middleware is really placed in the middleware directory?

Kind Regards