'Laravel : Method [show] does not exist
When trying to access this URL 'users/login' I got that error, Here is my code :
View users/login.blade.php :
<head>Sign in : </head>
<body>
{{ HTML::ul($errors->all()) }}
<?php echo Form::open(array('url' => 'users'));
echo '<div class="form-group">';
echo Form::label('username', 'User Name');
echo Form::text('ausername', null, array('class' => 'form-control'));
echo '</div>';
echo '<div class="form-group">';
echo Form::label('Password', 'Password');
echo Form::password('apassword', null, array('class' => 'form-control'));
echo '</div>';
echo Form::submit('Sign in', array('class' => 'btn btn-primary'));
echo Form::close();
?>
</body>
Controller Usercontroller.php
<?php
class UserController extends BaseController {
public function index()
{
$users = User::all();
return View::make('users.index')
->with('users', $users);
}
public function create()
{
return View::make('users.create');
}
public function store()
{
$rules = array(
'username' => 'required|alpha_dash',
'password' => 'required|confirmed',
'name' => 'required|regex:/^[a-zA-Z][a-zA-Z ]*$/',
'email' => 'required|email|unique:users',
'country' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('users/create')
->withErrors($validator)
->withInput(Input::except('password'));
} else {
$user = new User;
$user->username = Input::get('username');
$user->password = Hash::make(Input::get('password'));
$user->name = Input::get('name');
$user->email = Input::get('email');
$user->country = Input::get('country');
$user->save();
// redirect
Session::flash('message', 'Successfully created user!');
return Redirect::to('users');
}
}
public function login()
{
$reflector = new ReflectionClass("UserController");
$fn = $reflector->getFileName();
dd($fn);
return View::make('users.login');
}
public function authen()
{
if (Auth::attempt(array('username' => Input::get('ausername'), 'password' => Input::get('apassword'))))
{
return Redirect::intended('users');
}
}
}
and my routes.php
<?php
Route::resource('users','UserController');
Route::get('users/login', 'UserController@login');
Route::get('/', function()
{
return View::make('hello');
});
is it a route problem, thank you for the help
Solution 1:[1]
I have experienced the same problem as you. The problem ends up with rearranging the resource code, i.e.
Route::get('masterprices/data', 'MasterPriceController@data');
Route::get( 'masterprices/upload', 'MasterPriceController@upload');
Route::post('masterprices/upload', 'MasterPriceController@do_upload');
Route::get('masterprices/{masterprices}/multipledelete', 'MasterPriceController@multipledelete');
Route::resource('masterprices', 'MasterPriceController');
It checks the other possible handler, if none it will reach the last line which is resource to handle index page.
Solution 2:[2]
This one:
Route::resource('users','UserController');
defines following routes:
| GET|HEAD users | users.index | UsersController@index
| GET|HEAD users/create | users.create | UsersController@create
| POST users | users.store | UsersController@store
| GET|HEAD users/{users} | users.show | UsersController@show
| GET|HEAD users/{users}/edit | users.edit | UsersController@edit
| PUT users/{users} | users.update | UsersController@update
| PATCH users/{users} | | UsersController@update
| DELETE users/{users} | users.destroy | UsersController@destroy
So URI users/login calls users.show route and that's the problem.
Solution is like Kryten said to remove that route completely, but I suppose you still want to use some of the routes for the resource, like in your controller (create, store, index), so better use this:
Route::resource('users', 'UserController', ['only'=> ['index','create','store']]);
Solution 3:[3]
The problem list with the Route::resource call. By including that statement, you're telling Laravel that you want to use a RESTful controller for paths that start with users. This means that when you hit the URL 'users/login', the RESTful controller interprets that as a "show" action for the user controller and fails, since there's no show method. See http://laravel.com/docs/controllers#resource-controllers for details - the table on that page explains what routes are automatically configured when you implement a resource controller.
The solution is to remove Route::resource('users','UserController');.
Solution 4:[4]
Add the custom route before the resource routes in your routes file.
As per Laravel documentation:
If it becomes necessary to add additional routes to a resource controller beyond the default resource routes, you should define those routes before your call to Route::resource; otherwise, the routes defined by the resource method may unintentionally take precedence over your supplemental routes:
Solution 5:[5]
The simple solution is one I just found after reading your question. Define your ::resource routes after any ::get or ::post routes. I just tested on 4.2 and it's working (after having the same issue).
Solution 6:[6]
I was having same issue but it was weird one. In my routes file I had:
Route::controller('carts', 'CartsApiController');
When I changed it to:
Route::controller('cart', 'CartsApiController');
Everything was fine and I did not have the error again. I am not sure but seems like some naming issue.
Solution 7:[7]
I also have faced the same problem but i found it very dumb and silly mistake one use route like this : -
Route::get('users/login', ['uses'=>'UserController@login', 'as'=> 'userLogin']);
and in your view or controller use route('userLogin'); for route to that particular function in your controller
Solution 8:[8]
I know this is an old question but if somebody is still having this problem in Laravel 8,this worth for me:
Route::resource('tags',TagController::class)->except(['show']);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Frederick Li |
| Solution 2 | Jarek Tkaczyk |
| Solution 3 | Kryten |
| Solution 4 | Alex |
| Solution 5 | Qwiso |
| Solution 6 | Fraz Ahmed |
| Solution 7 | Willie Cheng |
| Solution 8 | Jonathan Hernández |
