hsntngr
153
8
Laravel

Laravel dosen't redirect request

Posted 4 months ago by hsntngr

I'm trying to do redirection in AppServiceProvider. if url doesn't have any lang parameter, I want to add lang parameter and redirect request to updated url.. But nothing happens.

public function boot(Request $request)
    {
        if (!$this->doNotLocalize($request)) {
            $langParameter = $request->segment(1);
            $supportedLanguages = config("translate.locales");
            if (strlen($langParameter) == 2) {
                $locale = $langParameter;
                if (in_array($locale, $supportedLanguages))
                    $this->app->setLocale($locale);
                else {
                    $this->app->setLocale(config("en"));
                }
            } else {
                $locale = in_array($request->getPreferredLanguage(), $supportedLanguages) ?
                    $request->getPreferredLanguage() : config("translate.locale");
                $redirectTo = $locale . $request->getPathInfo();
                // Save any flashed data for redirect
                app('session')->reflash();
                redirect($redirectTo, 302, ['Vary' => 'Accept-Language']);
            }
        }
   }

Am I doing wrong or are we not able to redirect requests in service providers ?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.