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);
}
}
localizationRedirect
is supposed to do? Maybe you can show us that if that's the case? – tptcatlocalizationRedirect
Middleware? – tptcat