php Laravel:方法[show]不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22943570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel : Method [show] does not exist
提问by Tarik Mokafih
When trying to access this URL 'users/login' I got that error, Here is my code :
尝试访问此 URL 'users/login' 时出现该错误,这是我的代码:
View users/login.blade.php :
查看 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
控制器 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
和我的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
是不是路线问题,谢谢帮忙
回答by Frederick Li
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.
它检查其他可能的处理程序,如果没有,它将到达处理索引页的资源的最后一行。
回答by Jarek Tkaczyk
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.
所以 URI users/login 调用 users.show 路由,这就是问题所在。
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:
解决方案就像 Kryten 所说的完全删除该路由,但我想您仍然希望使用一些路由来获取资源,例如在您的控制器中(创建、存储、索引),所以最好使用这个:
Route::resource('users', 'UserController', ['only'=> ['index','create','store']]);
回答by Kryten
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-controllersfor details - the table on that page explains what routes are automatically configured when you implement a resource controller.
Route::resource
呼叫的问题列表。通过包含该语句,您告诉 Laravel 您希望对以users
. 这意味着当您点击 URL 'users/login' 时,RESTful 控制器会将其解释为控制器的“显示”操作user
并失败,因为没有show
方法。有关详细信息,请参阅http://laravel.com/docs/controllers#resource-controllers- 该页面上的表格解释了实现资源控制器时自动配置的路由。
The solution is to remove Route::resource('users','UserController');
.
解决方法是删除Route::resource('users','UserController');
.
回答by Alex
Add the custom route before the resource routes in your routes file.
在路由文件中的资源路由之前添加自定义路由。
As per Laravel documentation:
根据 Laravel 文档:
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:
如果需要在默认资源路由之外向资源控制器添加其他路由,则应在调用 Route::resource 之前定义这些路由;否则,资源方法定义的路由可能会无意中优先于您的补充路由:
回答by Qwiso
The simple solution is one I just found after reading your question. Define your ::resource
routes afterany ::get
or ::post
routes. I just tested on 4.2 and it's working (after having the same issue).
简单的解决方案是我在阅读您的问题后刚刚找到的解决方案。定义你的::resource
路线后,任何::get
或::post
途径。我刚刚在 4.2 上进行了测试并且它正在工作(在遇到同样的问题之后)。
回答by Fraz Ahmed
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.
一切都很好,我没有再次出现错误。我不确定,但似乎有些命名问题。
回答by Devil Inside
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
并在您的视图或控制器中使用 route('userLogin'); 用于路由到控制器中的特定功能