Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel fails to find route except when the route is named

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 |
like image 700
uhexos Avatar asked Feb 25 '19 19:02

uhexos


People also ask

How can I solve this 404 Not Found error even when Route exist?

By adding the . htaccess file at the same folder location of the index. php it solves the 404 page not found error in my laravel Project.

What is Route model binding in Laravel?

Laravel route model binding provides a convenient way to automatically inject the model instances directly into your routes. For example, instead of injecting a user's ID, you can inject the entire User model instance that matches the given ID.

What is reverse routing in Laravel with example?

In Laravel reverse routing is generating URL's based on route declarations. Reverse routing makes your application so much more flexible. For example, the below route declaration tells Laravel to execute the action "login" in the user's controller when the request's URI is 'login'. Route::get('login', 'users@login');


1 Answers

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

like image 114
G-Man Avatar answered Oct 23 '22 04:10

G-Man