Laravel:BadMethodCallException 方法 [显示] 不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45656325/
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 : BadMethodCallException Method [show] does not exist
提问by crazyPen
(1/1) BadMethodCallException
Method [show] does not exist. in Controller.php (line 82)
(1/1) BadMethodCallException
方法 [show] 不存在。在 Controller.php 中(第 82 行)
I am new to Laravel and PHP and have been stuck on this error for a very long time with other questions not providing a solution. I was following an example (where the example worked) and made very little changes beside name changes.
我是 Laravel 和 PHP 的新手,并且很长一段时间都被困在这个错误上,其他问题没有提供解决方案。我正在遵循一个示例(示例工作的地方)并且除了名称更改之外做了很少的更改。
Here is the code:
这是代码:
web.php file
web.php 文件
Route::get('/', 'PagesController@home');
Route::get('faq', 'PagesController@faq');
Route::resource('support', 'UserInfoController');
UserInfoController.php
用户信息控制器.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\UserInfo;
class UserInfoController extends Controller
{
//
public function create(){
$userInfo = new UserInfo;
return view('contact', ['userInfo' => $userInfo]);
}
public function store(Request $request){
$this->validate($request, [
'name' => 'required',
'email' => 'required',
'subject' => 'required',
'description' => 'required',
]);
UserInfo::create($request->all());
return redirect()->route('contact')->with('success','Enquiry has been
submitted successfully');
}
}
UserInfo.php
用户信息.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserInfo extends Model {
protected $fillable = [
'name','email','subject','description',
];
}
The Route::resource
is the one giving me the problem as I am trying to access the page support/contact
. Would be very grateful if someone knew how to solve this.
The Route::resource
是在我尝试访问该页面时给我带来的问题support/contact
。如果有人知道如何解决这个问题,将不胜感激。
回答by Matias Carpintini
That is because you are doing resource routes in your routes.php file that generates all the routes for the CRUD functions when you have to generate a route for the show method you find that it does not exist. To solve it only creates the methods that you ask or, also you can define only the routes that you need.
那是因为你在你的 routes.php 文件中做资源路由,当你必须为你发现它不存在的 show 方法生成路由时,它会为 CRUD 函数生成所有路由。要解决它,只需创建您要求的方法,或者您也可以仅定义您需要的路线。
回答by Erik
The controller is trying to invoke the 'show' method - which you should have defined if you're going to load /support/{id}
via GET
in your browser. You can see the expected methods for a resource here:
控制器正在尝试调用 'show' 方法 - 如果您要/support/{id}
通过GET
浏览器加载,您应该定义该方法。您可以在此处查看资源的预期方法:
https://laravel.com/docs/5.4/controllers#resource-controllers
https://laravel.com/docs/5.4/controllers#resource-controllers
You can also make your life somewhat easier by starting with a valid controller by using the built in generator:
您还可以通过使用内置生成器从有效控制器开始,让您的生活更轻松:
php artisan make:controller UserInfoController --resource
If you don't want to supply ALL the methods, you have to specify, for example:
如果您不想提供所有方法,则必须指定,例如:
Route::resource('support', 'UserInfoController', ['only' => [
'create', 'store'
]]);
回答by Nour
Have you added method Show
to your Controller
? Route::Resource
has 7 basic routes:
你有没有Show
在你的Controller
? Route::Resource
有7条基本路线:
Verb Path Action Route Name
GET /support index support.index
GET /support/create create support.create
POST /support store support.store
GET /support/{support} show support.show
GET /support/{support}/edit edit support.edit
PUT /support/{support} update support.update
DELETE /support/{support} destroy support.destroy
As you see there is a route called show
which will be default when you route to support
so you must connect this route to it's method in the controller which is in resource
case CONTROLLER/show
, however in your case you're trying to get a static page from a prefix called support
which is different from resources
because show
in resource handling dynamic
results.
Use this syntax to get a page called contact
from prefix called support
如您所见,有一个称为show
路由的路由,当您路由到support
该路由时,该路由将成为默认路由,因此您必须将此路由连接到控制器中的方法,resource
以防万一CONTROLLER/show
,但是在您的情况下,您试图从名为的前缀获取静态页面support
这与资源处理结果中的resources
原因不同。使用此语法获取从名为的前缀调用的页面show
dynamic
contact
support
Route::prefix('support')->group(function () {
Route::get('contact', function () {
// Matches The "/UserInfoController/contact" URL
});
});