Reputation: 693
could you help me with my problem? I'm working with laravel5 App/Providers/RouteServiceProvider
. I'm trying to add to routes prefix in dependence on current lang. E.g.:
I have routes.php, where are routes like:
/blog
/home
And I need to do this: I url is for example:
/en/blog
I get the "lang" from url and I want to add routes prefix, to match it with my routes. So It will do smth. like this:
$router->group(['namespace' => $this->namespace, 'prefix' => $locale], function($router) {
require app_path('Http/routes.php');
});
I don't know why, but it doesn't work... if url is /en/blog
, It didn't match with any route, anyway it should...
I also tried to change namespace and in some controller method do this:
die(print_r(Route::getCurrentRoute()));
but namespace for all routes was: App\Controllers
Do you have an idea, why it doesn't work?
In my opinion, RouteServiceProvider isn't loaded, because also, when I try to put in method map
or method boot
die('smth');
it didn't do anything...
Here is my route service provider:
<?php namespace App\Providers;
use Illuminate\Routing\Router;
use Illuminate\Http\Request;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider {
/**
* This namespace is applied to the controller routes in your routes file.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function boot(Router $router)
{
parent::boot($router);
//
}
/**
* Define the routes for the application.
*
* @return void
*/
public function map(Router $router, Request $request)
{
$locale = $request->segment(1);
$this->app->setLocale($locale);
$router->group(['namespace' => $this->namespace, 'prefix' => $locale], function($router) {
require app_path('Http/routes.php');
});
}
}
Upvotes: 3
Views: 5447
Reputation: 33
Hey I see no one has responded to your question, I don't know if you have marked this as resolved but based on what I could tell:
php artisan route:cache
This means that Laravel is looking at the stored cache for the routes and not the routes.php or route service provider file you changed.
That means when I change the map function in RouteServiceProvider.php
nothing will happen
public function map(Router $router, Request $request)
{
/*
$locale = $request->segment(1);
$this->app->setLocale($locale);
$router->group(['namespace' => $this->namespace, 'prefix' => $locale], function($router) {
require app_path('Http/routes.php');
});
*/
}
You may already have already solved your problem by restarting your server however, a specific vendor file might be caching your routes for you.
php artisan route:clear
Afterwards all changes to your RouteServiceProvider.php
file should be immediate. So you can visit /en
but in order to force the prefix you need to add in a language middleware.
Searching around I think you were working off of the laracast blogs multiple locales post
php artisan make:middleware Language
and change its handle() functionAfter that just add in the Language middleware to Kernel.php
in order to force Laravel to route you to /en
protected $middleware = [
'App\Http\Middleware\Language'
];
Hope that solves your problem, better late than never
Upvotes: 1