Tomasz
Tomasz

Reputation: 1408

What is correct Route

I created language switcher but I have problem when is subpage. My route:

Route::post('/language', array(
    'Middleware' => 'LanguageSwitcher',
    'uses' => 'LanguageController@index'
));

Works fine for example:

http://localhost:8000/
http://localhost:8000/gallery

but not for;

http://localhost:8000/gallery/bodnar

then I recaive

MethodNotAllowedHttpException in RouteCollection.php line 218:

If I set:

Route::post('/gallery/language', array(
    'Middleware' => 'LanguageSwitcher',
    'uses' => 'LanguageController@index'
));

works for

http://localhost:8000/gallery/bodnar

but not for

http://localhost:8000/
http://localhost:8000/gallery

What is the correct Route::post to be universal?

my LanguageSwitcher.php

namespace App\Http\Middleware;

use Closure;

use App;
use Lang;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Config;

class LanguageSwitcher {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next) {
        App::setLocale(Session::has('locale') ? Session::get('locale') : Config::get('app.locale'));
        return $next($request);
    }

}

Upvotes: 1

Views: 85

Answers (1)

Sledmore
Sledmore

Reputation: 290

I think I understand what you need.

With your language switcher, I'm going to assume it's not an absolute path?

Perhaps use your first example:

Route::post('/language', array(
    'Middleware' => 'LanguageSwitcher',
    'uses' => 'LanguageController@index'
));

And for when calling that route use the following in blade:

{{ url('language') }}

This would prevent the directory issue.

Upvotes: 1

Related Questions