Laravel 5.0.* middleware to remove prefix locale from url before routes are processed

前提是你 提交于 2021-02-07 14:13:59

问题


I am looking for a way to make all app route's have multiple locales without using route groups. This is because I use an external extensions package, which means routes are registered in many places.

Essentially I want to have /foo/bar as well as /en/foo/bar, /de/foor/bar, /es/foo/bar etc all to be recognised and processed by the /foot/bar route

 Route::get('foo/bar', function () {
     return App::getLocale() . ' result';
 });

So the above would give me 'en result' or 'de result' or 'es result'.

I already have middleware that sets the locale based on the path segment. I have tried the following with no luck.

   ...
   $newPath =  str_replace($locale,'',$request->path());

   $request->server->set('REQUEST_URI',$new_path);

 }

 return $next($request);

Hopefully this is possible, or there is some other way of achieving it.

EDIT------

Based on a comment below I quickly hacked it by adding the following code into public/index.php. Hopefully this will give a better idea of what i'm trying to achieve by editing the request object.

$application_url_segments = explode( '/', trim( $_SERVER["REQUEST_URI"], '/' ) );

$application_locale = $application_url_segments[0];

$application_locales = ['en' => 'English', 'de' => 'German'];

if ( array_key_exists( $application_locale, $application_locales ) ) {

    $_SERVER["REQUEST_URI"] = str_replace( '/' . $application_locale,'',$_SERVER["REQUEST_URI"] );

}

回答1:


Here is the correct code to edit the URL before the routes get called.

<?php namespace App\Providers;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Request;

class LanguageServiceProvider extends ServiceProvider {
    public function register() {
      Request::instance()->server->set('REQUEST_URI',"/uri/");
    }
}

To note, fetching the path from the Request instance without duplicating it first will for some reason cause the REQUEST_URI to not be editable. I assume somewhere in the codebase laravel is initializing the request when you call the path() method.




回答2:


You can easily achieve this by hooking into the app a bit earlier. Create a ServiceProvider and create a register method and put your logic in there.

<?php namespace App\Providers;

use Illuminate\Support\ServiceProviders;
use Illuminate\Support\Facades\Request;

class LocaleServiceProvider extends ServiceProvider {

    // Fires during the registration of this ServiceProvider :)
    public function register(Request $request) {

        // Altar the Request object here
        // ...
        // ...

    }
}



回答3:


none of this actually worked for me with 5.5. The approach is good, but for me the request-argument did not get injected into the register method, likewise instance() is not a static method and should not be called as such.

However, using the service container to aquire an instance of Request made it possible to change the request-path before it is parsed inside the ServiceProviders register method:

public function register()
{
  $this->app->make('Illuminate\Http\Request')->instance()->server->set('REQUEST_URI',"/what/ever/");
}

I hope this helps somebody!

Cheers

edit: Pastor's answer is technically more correct as he uses the Facade instead of the actual Class. However, the injection of neither works on register but you can alternatively use:

use \Illuminate\Support\Facades\Request
//...
public function register()
{
  Request::instance()->server->set('REQUEST_URI',"/what/ever/");
}


来源:https://stackoverflow.com/questions/34865784/laravel-5-0-middleware-to-remove-prefix-locale-from-url-before-routes-are-proc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!