no reply?
Jul 5, 2023
3
Level 2
Why I'm getting 404 - Not Found while I add {locale} prefix to laravel breeze auth routes?
I have configured Laravel Breeze with localization correctly, but I am encountering a problem when accessing authentication routes like "login" and "register." Despite ensuring that the blade files and all necessary setups are in place, I consistently receive a 404 error.
here is my routes auth URL:
https://my-app.dev/en/register
I get 404 not found?
routes/web.php
<?php
use App\Http\Controllers\PageController;
use Illuminate\Support\Facades\Route;
// Redirect home with default language yourdomain.com/en/
Route::get('/', function () {
return redirect(app()->getLocale());
});
Route::prefix('{locale}')
->where(['locale' => '[a-zA-Z]{2}'])
->middleware('setlocale')
->group(function () {
Route::get('/{slug?}', [PageController::class, 'show'])
->name('page')
->where('slug', '.*');
require __DIR__.'/auth.php';
});
routes/auth.php
<?php
use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\Auth\RegisteredUserController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Auth Routes
|--------------------------------------------------------------------------
*/
Route::middleware('guest')->group(function () {
Route::get('register', [RegisteredUserController::class, 'create'])
->name('register');
Route::post('register', [RegisteredUserController::class, 'store']);
Route::get('login', [AuthenticatedSessionController::class, 'create'])
->name('login');
Route::post('login', [AuthenticatedSessionController::class, 'store']);
...
});
SetLocale.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\URL;
class SetLocale
{
/**
* Handle an incoming request.
*
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$locale = $request->segment(1);
$availableLocales = Config::get('app.available_locales');
if (! in_array($locale, $availableLocales)) {
$locale = Config::get('app.fallback_locale');
}
App::setLocale($locale);
URL::defaults(['locale' => $locale]);
return $next($request);
}
}
Please or to participate in this conversation.
