In Laravel 11, the approach to setting a default home route after login has changed slightly from previous versions. Instead of using a constant in the RouteServiceProvider, you can define the home route directly in your controller where you handle the post-login redirection, typically in the AuthenticatedSessionController.
Here's how you can set it:
-
Open the
AuthenticatedSessionControllerlocated atapp/Http/Controllers/Auth/AuthenticatedSessionController.php. -
In the
storemethod (or the method that handles the login), you can define the redirection path. If the method doesn't exist, you can override theauthenticatedmethod from the trait used in the controller.
Here's an example of how you might set the redirection path:
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AuthenticatedSessionController extends Controller
{
// ...
protected function authenticated(Request $request, $user)
{
return redirect()->intended('/dashboard'); // Change '/dashboard' to your desired path
}
// ...
}
If you're using the Fortify service provider, you can also define the home route by publishing the Fortify configuration and setting the home configuration option:
- Publish the Fortify configuration if you haven't already:
php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider"
-
Open the published
config/fortify.phpfile. -
Find the
homeconfiguration option and set your desired path:
'home' => '/dashboard',
Remember to replace '/dashboard' with the path you want users to be redirected to after login.
By following these steps, you should be able to set a custom home route in Laravel 11 after user authentication.