1
votes

PROBLEM: AJAX request not working with prefix

Website works great without language prefix. But I need it.. So before (no middleware) and ajax post to domain.com/entitlement worked great.

However, when posting with prefix (domain.com/en/entitlement) and having the pre-fix middleware on throws an error MethodNotAllowedHttpException in RouteCollection.php line 219:

The stackoverflow posts I have seen on prefix routing are focusing on GET related issues. Like Laravel 5 route prefix. I have a POST issue (the GET works fine)

Any ideas?

ROUTES

Route::group(['middleware' => ['web']], function () {

    Route::group(
    [
        'prefix' => LaravelLocalization::setLocale(),
       'middleware' => [ 'localeSessionRedirect', 'localizationRedirect' ]
    ],
    function()
    {

        Route::get('/', array(
            'as' => 'home',
            'uses' => 'HomeController@getHome'
        ));

        Route::post('/entitlement', array(
            'as' => 'entitlement-post',
            'uses' => 'HomeController@postEntlitment'
        ));
    }
}

AJAX REQUEST

$.ajax({
    type: 'POST',
    url: '/entitlement', --> Becomes domain.com/en/entitlement
    data: data,
    dataType:'json',
    beforeSend: function() { 
    },
...

LocalizationSessionRedirect

<?php namespace Mcamara\LaravelLocalization\Middleware;

use Illuminate\Http\RedirectResponse;
use Closure;

class LocaleSessionRedirect {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle( $request, Closure $next )
    {
        $params = explode('/', $request->path());
        $locale = session('locale', false);

        if ( count($params) > 0 && $locale = app('laravellocalization')->checkLocaleInSupportedLocales($params[ 0 ]) )
        {
            session([ 'locale' => $params[ 0 ] ]);

            return $next($request);
        }

        if ( $locale && app('laravellocalization')->checkLocaleInSupportedLocales($locale) && !( app('laravellocalization')->getDefaultLocale() === $locale && app('laravellocalization')->hideDefaultLocaleInURL() ) )
        {
            app('session')->reflash();
            $redirection = app('laravellocalization')->getLocalizedURL($locale);

            return new RedirectResponse($redirection, 302, [ 'Vary' => 'Accept-Language' ]);
        }

        return $next($request);
    }
}
1
Doesn't the URL in the Ajax request still need the language prefix or is that what localizationRedirect is supposed to do? Maybe you can show us that if that's the case?tptcat
@tptcat Thanks for quick response. The AJAX is built dynamically so the failed POST is domain.com/en/entitlement. Updated the question with the localizationSessionRedirect from Mcamara libraryPeder Wessel
Thanks @tptcat! Solved the issue!Peder Wessel
Was it removing the localizationRedirect Middleware?tptcat

1 Answers

0
votes

Thanks to tptcat the answer is to take out middleware of Mcmara's redirect.

UPDATED ROUTE

Route::group(
    [
        'prefix' => LaravelLocalization::setLocale(),
//       'middleware' => [ 'localeSessionRedirect', 'localizationRedirect' ] --> Not included
    ],
    function()
    {