Laravel fails to find route except when the route is named
Asked Answered
L

2

6

I'm trying to implement a basic authentication system using only the built-in Laravel features. The system works but only if I assign a name to the /admin/login route [i.e ...->name('admin.login)]. If I take it out, the exception I get is the following.

ErrorException (E_ERROR) Route [login.admin] not defined. (View: /var/www/html/shop/resources/views/auth/login.blade.php)

In the past, I have been able to visit unnamed routes, and I can visit other unnamed routes apart from the /login/admin. I was hoping someone could tell me why I was getting the error.

Routes

Auth::routes();

Route::get('/login/admin', 'Auth\LoginController@showAdminLoginForm')->name('login.admin');
Route::get('/login/staff', 'Auth\LoginController@showStaffLoginForm');
Route::get('/register/admin', 'Auth\RegisterController@showAdminRegisterForm');
Route::get('/register/staff', 'Auth\RegisterController@showStaffRegisterForm');

The $url can have 2 values either admin or staff.

login blade page

@isset($url)
   <form method="POST" action="{{ route('login.'.$url) }}">
@else
   <form method="POST" action="{{ route('login') }}">
@endisset

The command routes:list also shows that the route exists, just unnamed.

    |        | GET|HEAD  | login/admin              |                  | App\Http\Controllers\Auth\LoginController@showAdminLoginForm           | web,guest,guest:admin,guest:staff |
    |        | POST      | login/admin              |                  | App\Http\Controllers\Auth\LoginController@adminLogin                   | web,guest,guest:admin,guest:staff |
    |        | GET|HEAD  | login/writer             |                  | App\Http\Controllers\Auth\LoginController@showWriterLoginForm          | web,guest,guest:admin,guest:staff |
    |        | POST      | login/writer             |                  | App\Http\Controllers\Auth\LoginController@writerLogin                  | web,guest,guest:admin,guest:staff |
Lushy answered 25/2, 2019 at 19:35 Comment(5)
Does /login/admin exist in your Auth::routes() as well? That could be overriding your named route. Or try placing your named route above Auth::routes()Cockatoo
@Cockatoo i checked the routes generated by auth:routes and there were no clashing route the have overwritten it. I think G-Man answer was correct and I have marked it as such. Thank you for your help.Lushy
In your first para it is admin.login "but only if I assign a name to the /admin/login route [i.e ...->name('admin.login)]," and then in your routes it is login.admin ->name('login.admin'); ? What's going on?Leroylerwick
@Cockatoo i checked the routes generated by auth:routes and there were no clashing route the have overwritten it. I think G-Man answer was correct and I have marked it as such. Thank you for your help.Lushy
The inbuild laravel features already include a complete Authorization system.Schwejda
P
5

If I understand your question and what you're trying to accomplish...

In your login blade you use:

route('login.'.$url)

This requires the Route Name. Thats what throwing the error:

ErrorException (E_ERROR)
    Route [login.admin] not defined. (View: /var/www/html/shop/resources/views/auth/login.blade.php)

The route() function looks for the route name.

Instead of using the route path for the Action use the actual URL:

$url = Request::url();

since what you're trying to do is reload the same page.

( there are lots of ways to do this. This is just one way. But, the way you're doing it is a bit odd, IMHO - although it does work. )

Purple answered 25/2, 2019 at 19:51 Comment(3)
My understanding is that he was getting the error until he added the route name, the it worked.Purple
Ah, you may be right. I think the question needs to be made more clear.Caladium
To clear route cache and apply your route changes use: php artisan route:clearTightlipped
T
0

To clear route cache and apply your route changes use: php artisan route:clear

Tightlipped answered 7/7, 2022 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.