Create a Laravel Request object on the fly(动态创建 Laravel Request 对象)
问题描述
我正在一个控制器中处理数据,并希望将其进一步传递到另一个控制器中以避免重复代码.
有没有办法设置另一个控制器的 store 方法中需要的 Request 对象?我已经追踪了 Request 继承并找到了 Symfony 的 Request 对象,它有一个 request 属性,它实际上是一个 ParameterBag ,它包含一个方法 add> 添加带有值的参数.
我尝试了以下方法,但结果是 null:
$myRequest = new Request();$myRequest->request->add(['foo' =>'bar']);var_dump($myRequest->foo);我正在为这个项目使用 Laravel 5.1.
你可以使用replace():
$request = new IlluminateHttpRequest();$request->replace(['foo' =>'bar']);dd($request->foo);或者,为第二个控制器中发生的任何事情创建一个 Job 并删除 ShouldQueue 接口以使其同步运行会更有意义.>
希望这有帮助!
I'm handling data in one controller and want to pass it further into another controller to avoid duplicate code.
Is there a way to set up a Request object that is needed in the other controller's store-method? I've traced down the Request inheritance and came to Symfony's Request object which has a request property that is in fact a ParameterBag that holds a method add to add parameters with values to it.
I've tried the following but I'm getting null as result:
$myRequest = new Request();
$myRequest->request->add(['foo' => 'bar']);
var_dump($myRequest->foo);
I'm on Laravel 5.1 for this project.
You can use replace():
$request = new IlluminateHttpRequest();
$request->replace(['foo' => 'bar']);
dd($request->foo);
Alternatively, it would make more sense to create a Job for whatever is going on in your second controller and remove the ShouldQueue interface to make it run synchronously.
Hope this helps!
这篇关于动态创建 Laravel Request 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:动态创建 Laravel Request 对象
基础教程推荐
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何替换eregi() 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
