【发布时间】:2017-07-21 13:44:48
【问题描述】:
我发现自己陷入或迷失在文档中!
我正在使用 Laravel 5.4 并且 我正在尝试在需要请求对象的控制器中创建验证。
我的问题是我的路由传递参数,我在文档中找不到如何在 Controller 方法中包含 Request $request 参数和 $id 参数的示例。
这是我的例子:
1:SomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
...
public function edit($id) {
$request = Request; // <-- Do I use that instead of passing an arg?
}
2:在--routes/web.php中带有参数的路由
Route::match(['GET', 'POST'], '/some/edit/{id}', 'SomeController@edit');
在他们的示例@https://laravel.com/docs/5.4/requests#accessing-the-request 中,他们的控制器中有
Request $request这个,但是路由参数会发生什么?:
public function store(Request $request) {}
问题:我是否需要编辑路由,或者如果是请求,第一个参数是否会被忽略?
答:或者我会在 **SomeController.php 中这样做吗?**
public function edit(Request $request, $id)
{
// I would get the $request, but what happens to $id?
$this->validate($request, [
'title' => 'required|unique:posts|max:255',
]);
}
B:也许,像这样?:
public function edit($id)
{
// Or Request::getInstance()?
$this->validate(Request, [
'title' => 'required|unique:posts|max:255',
]);
}
任何建议都会有所帮助,甚至是一个例子!
【问题讨论】: