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 |
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
uhexos
  • 383
  • 4
  • 19
  • 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()` – aynber Feb 25 '19 at 19:39
  • @aynber 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. – uhexos Feb 25 '19 at 20:02
  • 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? – Iftikhar uddin Feb 25 '19 at 20:05
  • @aynber 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. – uhexos Feb 25 '19 at 20:07
  • 1
    The inbuild laravel features already include a complete Authorization system. – dparoli Feb 25 '19 at 20:23

2 Answers2

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. )

G-Man
  • 1,138
  • 16
  • 20
0

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