Laravel 5.0. * Middleware to remove locale prefix from URL to routes handled

I am looking for a way for an entire application route to have multiple locales without using route groups. This is because I use a package of external extensions, which means that 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. everything to be recognized and processed along the route / foot / bar

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

So the above will give me the result “result” or “result” or “result”.

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

  ... $newPath = str_replace($locale,'',$request->path()); $request->server->set('REQUEST_URI',$new_path); } return $next($request); 

I hope this is possible, or there is another way to achieve it.

EDIT ------

Based on the comment below, I quickly cracked it by adding the following code to public / index.php. Hope this gives 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"] ); } 
+5
source share
3 answers

Here is the correct code for editing URLs before calling routes.

 <?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/"); } } 

It should be noted that fetching from the Request instance without duplication will, for some reason, first cause REQUEST_URI to not be edited. I assume somewhere in the codebase laravel will initialize the request when you call the path () method.

+2
source

You can easily achieve this by connecting to the application a little earlier. Create a ServiceProvider and create a register method and place your logic 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 // ... // ... } } 
+1
source

none of this worked for me since 5.5. This approach is good, but for me the query argument was not entered into the register method, similarly instance() not a static method and should not be called as such.

However, using the service container to obtain the Request instance allowed changing the request path before its analysis in the ServiceProviders register method:

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

Hope this helps someone!

Greetings

edit: The pastor’s answer is technically more correct as it uses the Facade instead of the actual class. However, the injection does not work on register , but you can also use:

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

Source: https://habr.com/ru/post/1240882/


All Articles