将数组转换为 Laravel 5 中的请求对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36975479/
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
Convert array to Request object in laravel 5
提问by paranoid
I have store method in user controller like this
我在用户控制器中有这样的存储方法
public function store(Request $request)
{
User::create($request->all());
}
and in another controller I want use this method
在另一个控制器中我想使用这个方法
public function test(Request $request)
{
.....
app('\App\Http\Controllers\UserController')->store($test_array);
...
}
and $test_array
is:
并且$test_array
是:
{"name":"test","email":"[email protected]"}
and finally show me error this
最后告诉我这个错误
Argument 1 passed to App\Http\Controllers\UserController::store() must be an instance of Illuminate\Http\Request,
传递给 App\Http\Controllers\UserController::store() 的参数 1 必须是 Illuminate\Http\Request 的实例,
How can I convert array to Request object?
如何将数组转换为请求对象?
回答by KShport
Just use
只需使用
$request = new Illuminate\Http\Request($test_array);
回答by Parvez Rahaman
use $request->merge($someArray)
用 $request->merge($someArray)
You can try this way..
你可以试试这个方法。。
Your second controller's test method.
您的第二个控制器的测试方法。
public function test(Request $request)
{
$another_array = [];
$another_array['eg'] = $test_array;
$request->merge($another_array);
.....
app('\App\Http\Controllers\UserController')->store($request->get('eg'));
...
}
Your$test_array
must be of the type array
你$test_array
的类型必须是array
Hope this will solve your problem.
希望这能解决您的问题。
回答by Alexey Mezenin
I don't think you'll be able to use store()
this way.
我不认为你将能够使用store()
这种方式。
If you're calling store()
action from other places, you could place persisting logic into a model:
如果您store()
从其他地方调用操作,您可以将持久化逻辑放入模型中:
class User extends Model
public function storeData($data)
{
$createdUser = User::create($data);
return $createdUser;
}
And then call this from store()
action and test()
.
然后从store()
action 和 中调用它test()
。
If not just, just change store()
to:
如果不只是,只需更改store()
为:
public function store($data)
{
User::create($data);
}
回答by jakub_jo
How can I convert array to Request object?
如何将数组转换为请求对象?
If you really want to do it this way, which I wouldn't, here you're:
如果你真的想这样做,我不会这样做,你在这里:
Something like this should do the trick -- I did not test it:
这样的事情应该可以解决问题——我没有测试它:
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpFoundation\ParameterBag;
use Illuminate\Http\Request as IlluminateRequest;
$symfonyRequest = SymfonyRequest::createFromGlobals();
$symfonyRequest->query = new ParameterBag([
'foo' => 'bar',
]);
$request = IlluminateRequest::createFromBase($symfonyRequest);
回答by Rodrigo Souza
Why don't you use redirect using flashed session data? As the Response - Redirecting With Flashed Session Datadocumentation says:
为什么不使用闪存会话数据使用重定向?正如响应 - 使用 Flashed Session Data 重定向文档所说:
Redirecting to a new URL and flashing data to the session are typically done at the same time. So, for convenience, you may create a RedirectResponse instance and flash data to the session in a single method chain. This is particularly convenient for storing status messages after an action:
重定向到新 URL 和将数据写入会话通常是同时完成的。因此,为方便起见,您可以在单个方法链中创建一个 RedirectResponse 实例并将数据闪存到会话中。这对于在操作后存储状态消息特别方便:
Route::post('user/profile', function () {
// Update the user's profile...
return redirect('dashboard')->with('status', 'Profile updated!');
});
Of course, after the user is redirected to a new page, you may retrieve and display the flashed message from the session. For example, using Blade syntax:
当然,在用户重定向到新页面后,您可以检索并显示会话中闪现的消息。例如,使用 Blade 语法:
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
So in your case you would store return redirect('dashboard')->with('user', $userModel);
所以在你的情况下,你会存储 return redirect('dashboard')->with('user', $userModel);