vendredi 27 octobre 2017

Web middleware does not get executed (Laravel 5.5)

I'm trying to create a website with multiple languages (with Laravel 5.5). Therefore I follow the tutorial http://ift.tt/2oFqlIN, but somehow I have troubles with the language middleware part:

// BeforeLanguage.php 

namespace App\Http\Middleware;

use Closure;

class BeforeLanguage
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // Check if the first segment matches a language code
        if (!array_key_exists($request->segment(1), config('translatable.locales')) ) {

            // Store segments in array
            $segments = $request->segments();

            // Set the default language code as the first segment
            $segments = array_prepend($segments, config('app.fallback_locale'));

            // Redirect to the correct url
            return redirect()->to(implode('/', $segments));
        }

        return $next($request);
    }
}

It works if I open a URL with the identifier for the language, e. g. http://ift.tt/2lmU6Nz but I get "Sorry, the page you are looking for could not be found." if I try to open the page without the language identifier in the URI, e. g. http://ps.dev/app/login.

But here I would expect that the middleware adds the language segment to the URI. Any idea what could go wrong?

Below I would like to provide some additional information.

web.php

Route::prefix('app')->group(function () {

    // Authentication routes
    Route::get('login', 'SessionsController@create')->name('login');

    // ...
});

Kernel.php

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\BeforeLanguage::class,
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        // \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

According to the Laravel documentation page the middlewares assigned to the middleware group web get executed by default for any route defined inside web.php. (Out of the box, the web middleware group is automatically applied to your routes/web.php file by the RouteServiceProvider.)

RouteServiceProvider.php

protected function mapWebRoutes()
{

    $locale = Request::segment(1);

    Route::prefix($locale)
        ->middleware('web')
        ->namespace($this->namespace)
        ->group(base_path('routes/web.php'));

}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2yb6gPi
via IFTTT

Aucun commentaire:

Enregistrer un commentaire