Laravel : Method [show] does not exist
When you attempt to visit users/login and encounter a Method [show] does not exist error, the culprit is rarely the specific route definition or the controller method signature. This error stems from how Laravel's routing engine resolves resource patterns versus explicit route definitions when they coexist in the same application context.
The root cause lies in the interaction between your Route::resource('users','UserController') declaration and the subsequent Route::get('users/login', 'UserController@login'). By defining a resource group for users, you have automatically registered a set of standard routes: index, create, store, show, edit, update, and delete. These map to HTTP verbs (GET, POST) for generic CRUD operations. Crucially, the resource helper also registers a GET /users/{id} route that points to the show method of your controller.
If you intend to treat /users/login as part of this resource group, you must ensure the route name and HTTP verb align perfectly with what Laravel expects. However, the error message specifically suggests that the framework is attempting to resolve a request to GET /users/login against a route pattern that expects a method named show. This typically happens when you inadvertently create a conflict or if the routing order causes Laravel to interpret the path /users/login as an attempt to access a resource item with an ID of "login".
In your provided code, the route definition Route::get('users/login', 'UserController@login') is syntactically correct for defining a static page. However, if you are seeing the [show] error, it implies that somewhere in your routing setup or middleware logic, Laravel is trying to match this request against the resource routes generated by Route::resource.
A common pitfall occurs when developers mix resource routes with custom paths without careful segmentation. If you define Route::resource('users'), Laravel expects all subsequent requests starting with /users/ to follow the standard resource pattern unless explicitly overridden. While your explicit GET route should take precedence due to order, ensuring that the controller method names match the expected HTTP verbs is essential. The login method in your controller is being invoked correctly by the explicit route, but if there is a mismatch in how the view is being rendered or if an alias is involved, the framework might misinterpret the request type.
To resolve this cleanly, consider decoupling the authentication logic from the resource CRUD operations. The /users prefix suggests a resource context, but login pages are stateless views that do not fit the show, edit, or update paradigms of a standard model resource. You have two robust options to fix the architecture:
- Explicit Route Segmentation: Ensure your explicit route comes before any potential conflicting definitions in your
routes.php. More importantly, verify that you haven't inadvertently created a named route alias or middleware redirection that forces ashowmethod call. - Refactoring the Controller: Your current
loginmethod returns a view directly. This is fine, but ensure the route name provided to Laravel does not clash with resource expectations. You can explicitly name your custom route to avoid ambiguity:Route::get('users/login', ['uses' => 'UserController@login', 'as' => 'login']);
Additionally, check your view file users/login.blade.php. The use of HTML::ul() and Form::open() suggests you are using the legacy Blade helper syntax. While this works, it is worth noting that in newer Laravel versions, these static helpers were replaced by the Blade facade or native HTML generation methods. If you are upgrading your codebase, ensure you are not calling deprecated methods that might throw unexpected exceptions resembling routing errors.
Finally, verify your authen method logic. You are using Auth::attempt() with specific field names (username and password). Ensure that the database columns match exactly, including case sensitivity depending on your driver. If authentication fails repeatedly, Laravel's session handling might interfere with redirect flows, but it will not cause a [show] error unless the route resolution itself is confused.
By separating the login flow from the standard resource CRUD routes and ensuring explicit route definitions take precedence, you eliminate the ambiguity that triggers the [show] method error. This architectural choice makes your application more maintainable and prevents future conflicts as you expand the user management features.