Laravel 5.4 带有请求和参数的路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42545153/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:27:00  来源:igfitidea点击:

Laravel 5.4 Route with Request and Params

phplaravel

提问by JREAM

I found myself stuck or lost in docs!

我发现自己卡在或迷失在文档中!

I'm using Laravel 5.4and I'm trying to create validation in a controllerwhich requires a request object.

我正在使用Laravel 5.4我正在尝试在需要请求对象的控制器中创建验证

My problem is my route passes parameters, I can't find in the docs an example how you include the Request $requestargument and the $idparameter in a Controller method.

我的问题是我的路由传递参数,我在文档中找不到如何在 Controller 方法中包含Request $request参数和$id参数的示例。

Here is my example:

这是我的例子:

1: SomeController.php

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 with a Paramter in -- routes/web.php

2:带有参数的路由--routes/web.php

Route::match(['GET', 'POST'], '/some/edit/{id}', 'SomeController@edit');

In their example @ https://laravel.com/docs/5.4/requests#accessing-the-requestthey have Request $requestthis in their controller, but what happens to Route parameters?:

在他们的示例@ https://laravel.com/docs/5.4/requests#accessing-the-requestRequest $request中,他们的控制器中有这个,但是路由参数会发生什么?

public function store(Request $request) {}


Question: Do I need to edit the route or is the first parameter ignored if its a request?

问题:我是否需要编辑路由,或者如果它是一个请求,第一个参数是否被忽略?



A: Or would I do this in **SomeController.php?**

A: 或者我会在 **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: Perhaps, something like this?:

B:也许,像这样?:

public function edit($id)
{
    // Or Request::getInstance()?
    $this->validate(Request, [
        'title' => 'required|unique:posts|max:255',
    ]);
}

Any advice would help, or even an example!

任何建议都会有所帮助,甚至是一个例子!

回答by EddyTheDove

Doing

正在做

public function edit(Request $request, $id)

should match the route you already have. So it's ok to do that. Request $requesthere is not a required parameter that you pass. Laravel will take care of it.

应该与您已有的路线相匹配。所以这样做是可以的。Request $request这里不是您传递的必需参数。Laravel 会处理它。

It works this way because everytime you send a request, well Laravel 'sends' a request for you (GET, POST). So, it always exist. But you can use use it within your controller if you dont pass it to the controller function as a parameter.

它以这种方式工作,因为每次您发送请求时,Laravel 都会为您“发送”一个请求(GET、POST)。所以,它一直存在。但是如果你不将它作为参数传递给控制器​​函数,你可以在你的控制器中使用它。

However, you can access it through the global helper function request()if you dont want to pass it as a function parameter.

但是,request()如果您不想将其作为函数参数传递,则可以通过全局辅助函数访问它。

$request = request();
$name = $request->name; // or 
$name = request('name');