#Middleware does not exist

15 messages · Page 1 of 1 (latest)

sleek kiln
#

Hello everyone!

I've been working with Laravel for a short period of time, and I've encountered an issue for which I can't find a solution. I'm trying to implement localization, but every time I create a middleware and access my website, I receive the error message that it doesn't exist.

[previous exception] [object] (ReflectionException(code: -1): Class \"App\\Http\\Middleware\\LanguageManager\" does not exist
[2023-10-10 21:01:33] production.ERROR: Target class [App\Http\Middleware\LanguageManager] does not exist.```

Does anyone have an idea of what might be causing this? 
Thank you!
gilded oyster
#

Double check the namespace and casing of the files

sleek kiln
#

I already have that, everything agrees

gilded oyster
#

Well, the class doesn't exist. So either the PSR-4 namespace etc is wrong, or the file actually doesn't exist.
Also, are you doing development on a production server...?! Why not set things up locally instead of editing directly on a server?

sleek kiln
#

I edited this local with XAMPP

gilded oyster
#

Then why is the environment set to production..? Usually a production environment would have all kinds of caching, such as opcache, so changing/adding files might've no effect.

sleek kiln
#

Okay, I wasn't aware of that. I'm sorry. I only came across it yesterday.

#

[22:22:07] LOG.error: Target class [App\Http\Middleware\LanguageManager] does not exist. {
"exception": {}
}

my file in App\Http\Middleware\LanguageManager.php

  
namespace App\Http\Middleware;
  
use Closure;
use App;
  
class LanguageManager
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (session()->has('locale')) {
            App::setLocale(session()->get('locale'));
        }
          
        return $next($request);
    }
}```
#

\App\Http\Middleware\LanguageManager::class,
in Kernel.php $middlewareGroups 'web'

worldly portal
#

just created a middleware for an example - request won't/may not work in your example - but also how does it look in routes file?

#
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class SomeMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        return $next($request);
    }
}```
#

also in kernel did you set alias if you're using it in route group..

sleek kiln
#

Perhaps I've been looking at an outdated tutorial on Localization and Language Switcher.

worldly portal
#

I think it should not be here in Kernel.php $middlewareGroups 'web' but kernel $middleware for global