Skip to content

Commit c388856

Browse files
Add ability to override routes with custom paths (#458)
* Add override routes with custom names * Update formatting * Update formatting again * Move import statement to bottom * Update route 'name' to 'path' * formatting * Update RoutePath.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 7800dcb commit c388856

File tree

3 files changed

+68
-26
lines changed

3 files changed

+68
-26
lines changed

config/fortify.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,29 @@
1616
'limiters' => [
1717
'login' => null,
1818
],
19+
'paths' => [
20+
'login' => null,
21+
'logout' => null,
22+
'password.request' => null,
23+
'password.reset' => null,
24+
'password.email' => null,
25+
'password.update' => null,
26+
'register' => null,
27+
'verification.notice' => null,
28+
'verification.verify' => null,
29+
'verification.send' => null,
30+
'user-profile-information.update' => null,
31+
'user-password.update' => null,
32+
'password.confirm' => null,
33+
'password.confirmation' => null,
34+
'two-factor.login' => null,
35+
'two-factor.enable' => null,
36+
'two-factor.confirm' => null,
37+
'two-factor.disable' => null,
38+
'two-factor.qr-code' => null,
39+
'two-factor.secret-key' => null,
40+
'two-factor.recovery-codes' => null,
41+
],
1942
'redirects' => [
2043
'login' => null,
2144
'logout' => null,

routes/routes.php

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
use Laravel\Fortify\Http\Controllers\TwoFactorQrCodeController;
2020
use Laravel\Fortify\Http\Controllers\TwoFactorSecretKeyController;
2121
use Laravel\Fortify\Http\Controllers\VerifyEmailController;
22+
use Laravel\Fortify\RoutePath;
2223

2324
Route::group(['middleware' => config('fortify.middleware', ['web'])], function () {
2425
$enableViews = config('fortify.views', true);
2526

2627
// Authentication...
2728
if ($enableViews) {
28-
Route::get('/login', [AuthenticatedSessionController::class, 'create'])
29+
Route::get(RoutePath::for('login', '/login'), [AuthenticatedSessionController::class, 'create'])
2930
->middleware(['guest:'.config('fortify.guard')])
3031
->name('login');
3132
}
@@ -34,102 +35,102 @@
3435
$twoFactorLimiter = config('fortify.limiters.two-factor');
3536
$verificationLimiter = config('fortify.limiters.verification', '6,1');
3637

37-
Route::post('/login', [AuthenticatedSessionController::class, 'store'])
38+
Route::post(RoutePath::for('login', '/login'), [AuthenticatedSessionController::class, 'store'])
3839
->middleware(array_filter([
3940
'guest:'.config('fortify.guard'),
4041
$limiter ? 'throttle:'.$limiter : null,
4142
]));
4243

43-
Route::post('/logout', [AuthenticatedSessionController::class, 'destroy'])
44+
Route::post(RoutePath::for('logout', '/logout'), [AuthenticatedSessionController::class, 'destroy'])
4445
->name('logout');
4546

4647
// Password Reset...
4748
if (Features::enabled(Features::resetPasswords())) {
4849
if ($enableViews) {
49-
Route::get('/forgot-password', [PasswordResetLinkController::class, 'create'])
50+
Route::get(RoutePath::for('password.request', '/forgot-password'), [PasswordResetLinkController::class, 'create'])
5051
->middleware(['guest:'.config('fortify.guard')])
5152
->name('password.request');
5253

53-
Route::get('/reset-password/{token}', [NewPasswordController::class, 'create'])
54+
Route::get(RoutePath::for('password.reset', '/reset-password/{token}'), [NewPasswordController::class, 'create'])
5455
->middleware(['guest:'.config('fortify.guard')])
5556
->name('password.reset');
5657
}
5758

58-
Route::post('/forgot-password', [PasswordResetLinkController::class, 'store'])
59+
Route::post(RoutePath::for('password.email', '/forgot-password'), [PasswordResetLinkController::class, 'store'])
5960
->middleware(['guest:'.config('fortify.guard')])
6061
->name('password.email');
6162

62-
Route::post('/reset-password', [NewPasswordController::class, 'store'])
63+
Route::post(RoutePath::for('password.update', '/reset-password'), [NewPasswordController::class, 'store'])
6364
->middleware(['guest:'.config('fortify.guard')])
6465
->name('password.update');
6566
}
6667

6768
// Registration...
6869
if (Features::enabled(Features::registration())) {
6970
if ($enableViews) {
70-
Route::get('/register', [RegisteredUserController::class, 'create'])
71+
Route::get(RoutePath::for('register', '/register'), [RegisteredUserController::class, 'create'])
7172
->middleware(['guest:'.config('fortify.guard')])
7273
->name('register');
7374
}
7475

75-
Route::post('/register', [RegisteredUserController::class, 'store'])
76+
Route::post(RoutePath::for('register', '/register'), [RegisteredUserController::class, 'store'])
7677
->middleware(['guest:'.config('fortify.guard')]);
7778
}
7879

7980
// Email Verification...
8081
if (Features::enabled(Features::emailVerification())) {
8182
if ($enableViews) {
82-
Route::get('/email/verify', [EmailVerificationPromptController::class, '__invoke'])
83+
Route::get(RoutePath::for('verification.notice', '/email/verify'), [EmailVerificationPromptController::class, '__invoke'])
8384
->middleware([config('fortify.auth_middleware', 'auth').':'.config('fortify.guard')])
8485
->name('verification.notice');
8586
}
8687

87-
Route::get('/email/verify/{id}/{hash}', [VerifyEmailController::class, '__invoke'])
88+
Route::get(RoutePath::for('verification.verify', '/email/verify/{id}/{hash}'), [VerifyEmailController::class, '__invoke'])
8889
->middleware([config('fortify.auth_middleware', 'auth').':'.config('fortify.guard'), 'signed', 'throttle:'.$verificationLimiter])
8990
->name('verification.verify');
9091

91-
Route::post('/email/verification-notification', [EmailVerificationNotificationController::class, 'store'])
92+
Route::post(RoutePath::for('verification.send', '/email/verification-notification'), [EmailVerificationNotificationController::class, 'store'])
9293
->middleware([config('fortify.auth_middleware', 'auth').':'.config('fortify.guard'), 'throttle:'.$verificationLimiter])
9394
->name('verification.send');
9495
}
9596

9697
// Profile Information...
9798
if (Features::enabled(Features::updateProfileInformation())) {
98-
Route::put('/user/profile-information', [ProfileInformationController::class, 'update'])
99+
Route::put(RoutePath::for('user-profile-information.update', '/user/profile-information'), [ProfileInformationController::class, 'update'])
99100
->middleware([config('fortify.auth_middleware', 'auth').':'.config('fortify.guard')])
100101
->name('user-profile-information.update');
101102
}
102103

103104
// Passwords...
104105
if (Features::enabled(Features::updatePasswords())) {
105-
Route::put('/user/password', [PasswordController::class, 'update'])
106+
Route::put(RoutePath::for('user-password.update', '/user/password'), [PasswordController::class, 'update'])
106107
->middleware([config('fortify.auth_middleware', 'auth').':'.config('fortify.guard')])
107108
->name('user-password.update');
108109
}
109110

110111
// Password Confirmation...
111112
if ($enableViews) {
112-
Route::get('/user/confirm-password', [ConfirmablePasswordController::class, 'show'])
113+
Route::get(RoutePath::for('password.confirm', '/user/confirm-password'), [ConfirmablePasswordController::class, 'show'])
113114
->middleware([config('fortify.auth_middleware', 'auth').':'.config('fortify.guard')]);
114115
}
115116

116-
Route::get('/user/confirmed-password-status', [ConfirmedPasswordStatusController::class, 'show'])
117+
Route::get(RoutePath::for('password.confirmation', '/user/confirmed-password-status'), [ConfirmedPasswordStatusController::class, 'show'])
117118
->middleware([config('fortify.auth_middleware', 'auth').':'.config('fortify.guard')])
118119
->name('password.confirmation');
119120

120-
Route::post('/user/confirm-password', [ConfirmablePasswordController::class, 'store'])
121+
Route::post(RoutePath::for('password.confirm', '/user/confirm-password'), [ConfirmablePasswordController::class, 'store'])
121122
->middleware([config('fortify.auth_middleware', 'auth').':'.config('fortify.guard')])
122123
->name('password.confirm');
123124

124125
// Two Factor Authentication...
125126
if (Features::enabled(Features::twoFactorAuthentication())) {
126127
if ($enableViews) {
127-
Route::get('/two-factor-challenge', [TwoFactorAuthenticatedSessionController::class, 'create'])
128+
Route::get(RoutePath::for('two-factor.login', '/two-factor-challenge'), [TwoFactorAuthenticatedSessionController::class, 'create'])
128129
->middleware(['guest:'.config('fortify.guard')])
129130
->name('two-factor.login');
130131
}
131132

132-
Route::post('/two-factor-challenge', [TwoFactorAuthenticatedSessionController::class, 'store'])
133+
Route::post(RoutePath::for('two-factor.login', '/two-factor-challenge'), [TwoFactorAuthenticatedSessionController::class, 'store'])
133134
->middleware(array_filter([
134135
'guest:'.config('fortify.guard'),
135136
$twoFactorLimiter ? 'throttle:'.$twoFactorLimiter : null,
@@ -139,31 +140,31 @@
139140
? [config('fortify.auth_middleware', 'auth').':'.config('fortify.guard'), 'password.confirm']
140141
: [config('fortify.auth_middleware', 'auth').':'.config('fortify.guard')];
141142

142-
Route::post('/user/two-factor-authentication', [TwoFactorAuthenticationController::class, 'store'])
143+
Route::post(RoutePath::for('two-factor.enable', '/user/two-factor-authentication'), [TwoFactorAuthenticationController::class, 'store'])
143144
->middleware($twoFactorMiddleware)
144145
->name('two-factor.enable');
145146

146-
Route::post('/user/confirmed-two-factor-authentication', [ConfirmedTwoFactorAuthenticationController::class, 'store'])
147+
Route::post(RoutePath::for('two-factor.confirm', '/user/confirmed-two-factor-authentication'), [ConfirmedTwoFactorAuthenticationController::class, 'store'])
147148
->middleware($twoFactorMiddleware)
148149
->name('two-factor.confirm');
149150

150-
Route::delete('/user/two-factor-authentication', [TwoFactorAuthenticationController::class, 'destroy'])
151+
Route::delete(RoutePath::for('two-factor.disable', '/user/two-factor-authentication'), [TwoFactorAuthenticationController::class, 'destroy'])
151152
->middleware($twoFactorMiddleware)
152153
->name('two-factor.disable');
153154

154-
Route::get('/user/two-factor-qr-code', [TwoFactorQrCodeController::class, 'show'])
155+
Route::get(RoutePath::for('two-factor.qr-code', '/user/two-factor-qr-code'), [TwoFactorQrCodeController::class, 'show'])
155156
->middleware($twoFactorMiddleware)
156157
->name('two-factor.qr-code');
157158

158-
Route::get('/user/two-factor-secret-key', [TwoFactorSecretKeyController::class, 'show'])
159+
Route::get(RoutePath::for('two-factor.secret-key', '/user/two-factor-secret-key'), [TwoFactorSecretKeyController::class, 'show'])
159160
->middleware($twoFactorMiddleware)
160161
->name('two-factor.secret-key');
161162

162-
Route::get('/user/two-factor-recovery-codes', [RecoveryCodeController::class, 'index'])
163+
Route::get(RoutePath::for('two-factor.recovery-codes', '/user/two-factor-recovery-codes'), [RecoveryCodeController::class, 'index'])
163164
->middleware($twoFactorMiddleware)
164165
->name('two-factor.recovery-codes');
165166

166-
Route::post('/user/two-factor-recovery-codes', [RecoveryCodeController::class, 'store'])
167+
Route::post(RoutePath::for('two-factor.recovery-codes', '/user/two-factor-recovery-codes'), [RecoveryCodeController::class, 'store'])
167168
->middleware($twoFactorMiddleware);
168169
}
169170
});

src/RoutePath.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Laravel\Fortify;
4+
5+
class RoutePath
6+
{
7+
/**
8+
* Get the route path for the given route name.
9+
*
10+
* @param string $routeName
11+
* @param string $default
12+
* @return string
13+
*/
14+
public static function for(string $routeName, string $default)
15+
{
16+
return config('fortify.paths.'.$routeName) ?? $default;
17+
}
18+
}

0 commit comments

Comments
 (0)