Closed JerryBels closed 4 years ago
You can change it in the "vendor/laravel/jetstream/routes" path..
@semihsemih You wouldn't edit a library file... The only option I see would be to copy / paste the routes in my routes/web.php
file and change it to fit my needs there. And this would force me to change lots of things, which would defeat the reason of using jetstream and fortify in the first place.
Finally successfully nailed this. I simply disabled routes from Fortify and Jetstream, copied them over and shoved them inside my grouped prefix routes.
In JetstreamServiceProvider :
public function register() {
Jetstream::ignoreRoutes();
}
In FortifyServiceProvider :
public function register() {
Fortify::ignoreRoutes();
}
And copy over routes from Fortify vendor/laravel/fortify/routes/routes.php
and Jetstream vendor/laravel/jetstream/routes/livewire.php
(I guess adapt to Inertia if you're working with this) over to your web.php
file, inside a route group with the prefix you need (LaravelLocalization::setLocale()
in this case).
I tried that. Before I run php artisan route:trans:cache
I can see verification.send|notice|verify routes (for example) but afterwards I couldn't.
I disabled the fortify and jetstream routes and copied over the route files into my own routes/ directory and included them like this in my routes/web.php config:
require_once __DIR__ . '/fortify/routes.php';
require_once __DIR__ . '/jetstream/livewire.php';
So they are wrapped properly now:
Route::group(
[
'prefix' => LaravelLocalization::setLocale(),
'middleware' => ['web', 'localeCookieRedirect', 'localizationRedirect', 'localeViewPath'],
],
function () {
$enableViews = config('fortify.views', true);
// Authentication...
if ($enableViews) {
Route::get('/login', [AuthenticatedSessionController::class, 'create'])
->middleware(['guest'])
->name('login');
}
// ...
But as soon if I cache the routes I can not see them anymore. Neither with php artisan route:trans:list en |grep login
nor with php artisan route:list |grep login
Finally successfully nailed this. I simply disabled routes from Fortify and Jetstream, copied them over and shoved them inside my grouped prefix routes.
In JetstreamServiceProvider :
public function register() { Jetstream::ignoreRoutes(); }
In FortifyServiceProvider :
public function register() { Fortify::ignoreRoutes(); }
And copy over routes from Fortify
vendor/laravel/fortify/routes/routes.php
and Jetstreamvendor/laravel/jetstream/routes/livewire.php
(I guess adapt to Inertia if you're working with this) over to yourweb.php
file, inside a route group with the prefix you need (LaravelLocalization::setLocale()
in this case).
Don't work on my side. The prefix working but after a login or logout (even if the routes are in my web.php). It's return the website to default language.
@Brouilles works super fine for me.
Btw if you want to make it even cleaner. Don't use all the routes in web.php It's a mess if you do it. Make it better like this:
// RouteServiceProvider.php
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/fortify.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/inertia.php'));
});
}
Hello @JerryBels thanks it help me this solution , the only problem I currently have is with the redirect after login, it does not add the /prefix for example /en it take me to the normal url http://localhost.com/dashboard how did you solve this?
Late to the party, but: in both jetstream and fortify, let's take Jetstream for example, you can run Jetstream::ignoreRoutes();
in JetstreamServiceProvider.
Then, call a method that registers the routes; it is a copy/paste of the code in JetstreamServiceProvider in vendor, with adjusted route path and, as you can see, prefix.
Also, for the redirection issue, in jetstream.php, in config, update middleware to 'middleware' => ['web', 'localeSessionRedirect', 'localizationRedirect']
or whatever else works for you. This will be applied in the routes file.
private function registerRoutes(): void
{
Route::group([
'namespace' => 'Laravel\Jetstream\Http\Controllers',
'domain' => config('jetstream.domain', null),
'prefix' => LaravelLocalization::setLocale().config('jetstream.prefix', config('jetstream.path')),
], function () {
$this->loadRoutesFrom(base_path().'/vendor/laravel/jetstream/routes/'.config('jetstream.stack').'.php');
});
}
With these changes, it seems to load and redirect properly.
Note: calling LaravelLocalization::setLocale() in 3 places (including web.php) seems overkill and a potential cause for problems, because it runs the detection steps every time. It seems ok to call it only when loading the fortify routes and for jetstream and web.php call LaravelLocalization::getCurrentLocale() instead.
This is needed for the redirect after logout, in the register()
of FortifyServiceProvider
$this->app->instance(LogoutResponse::class, new class implements LogoutResponse {
public function toResponse($request)
{
return $request->wantsJson()
? new JsonResponse('', 204)
: redirect(LaravelLocalization::localizeUrl(Fortify::redirects('logout', '/')));
}
});
An alternative for Laravel 11:
->booting(function () {
$routeLocale = LaravelLocalization::setLocale();
Config::set('fortify.prefix', ($routeLocale ?: '').config('fortify.prefix', ''));
Config::set('jetstream.prefix', ($routeLocale ?: '').config('jetstream.prefix', ''));
\App\Providers\AppServiceProvider::$routeLocale = $routeLocale;
})
Add this in boostrap/app.php. And that's it. Use AppServiceProvider::$routeLocale in web.php, instead of LaravelLocalization::setLocale(). No need to touch Fortify or Jetstream.
The piece of code for logout redirect handling in the previous comment is still needed though.
Describe the bug
I started a project with JetStream. Routes for authentication and such are automatically handled by JetStream / Fortify. I then added laravel-localization to handle the... Well... Localization. it works fine for the routes I created myself :
But how can I set the group, prefix and middleware on the routes handled by Jetstream and Fortify? Is it even possible?
More info: