Laravel 重定向->back() 返回空参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37060276/
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 redirect->back() return empty params
提问by Funny Frontend
I have a index.blade.phppage, I have a this code that shows alerts success or errors:
我有一个index.blade.php页面,我有一个显示警报成功或错误的代码:
<!-- ALERTS -->
@if (isset($msg))
@if ($msg_type == 'error')
<div id="alert" class="alert alert-danger" role="alert">{{ $msg }}</div>
@else
<div id="alert" class="alert alert-success" role="alert">{{ $msg }}</div>
@endif
<script>setTimeout("document.getElementById('mensajecarrito').style.display='none'",6000);</script>
@endif
<!-- //ALERTS -->
This index have a form, and this form url POST to the post_designs url, and this is his controller method:
这个索引有一个表单,这个表单 url POST 到 post_designs url,这是他的控制器方法:
public function postDesign() {
if (Session::has('FUNCTION'))
$function = Session::get('FUNCTION');
if (Input::has('FUNCTION'))
$function = Input::get('FUNCTION');
if (isset($function))
{
switch($function)
{
case 'createDesign':
try
{
//do something good
$msg_type = 'success';
$msg = 'Design created successfully';
}
catch(FotiApiException $e)
{
$msg_type = 'error';
$msg = 'Unexpected error';
}
break;
}
}
return back()->with(array('msg' => $msg, 'msg_type' => $msg_type));
}
The problem is, that the $msg and $msg_type variables are empty when return back to my index.blade.php...
问题是,当返回到我的 index.blade.php 时, $msg 和 $msg_type 变量是空的...
I don′t understand, my project is Laravel 5.2.3 and I have the correct route Middleware:
我不明白,我的项目是 Laravel 5.2.3 并且我有正确的路由中间件:
Route::group(['middleware' =>[ 'web']], function () {
# Index
Route::get('/',['as'=> 'index','uses' => 'WebController@getIndex']);
// # POSTS
Route::post('/post-design', ['as' => 'post_design', 'uses' => 'WebController@postDesign']);
});
回答by Mahesh Yadav
If you want to send some extra GET parameters then simply follow below.
如果你想发送一些额外的 GET 参数,那么只需按照下面的操作。
$previousUrl = app('url')->previous();
return redirect()->to($previousUrl.'?'. http_build_query(['varName'=>'varValue']));
It will redirect with yoururl?varName=varValue, hope it helps!!
它将使用yoururl?varName=varValue重定向,希望它有所帮助!!
回答by u10912195
you can use withInput()
你可以使用 withInput()
return back()->withInput(array('msg' => $msg, 'msg_type' => $msg_type));
the data saved in session if you want to use the data in blade. try use old('msg') or old('msg_type')
如果要使用刀片中的数据,请使用会话中保存的数据。尝试使用 old('msg') 或 old('msg_type')
@if(old('msg'))
<p>msg:{{ old('msg') }}<p>
@endif
php
php
$msg = $request->old('msg');
回答by Owais Majid
I'm new with laravel 5.2, about routes there is no need to but routes in
我是 Laravel 5.2 的新手,关于路线没有必要,但路线
['middleware' =>[ 'web']]
scene all routes in laravel 5.2 goes in it , so try to remove it
场景 Laravel 5.2 中的所有路由都包含在其中,因此请尝试将其删除
and to return view with 2 values try to make it like that :
并返回带有 2 个值的视图,尝试使其像这样:
return back()->with('msg', $msg)->with('msg_type', $msg_type);
I hope that fixes the problem :)
我希望能解决问题:)
回答by Marc Rovira
Instead of using
而不是使用
return back()->with('msg', $msg)->with('msg_type', $msg_type);
use
用
return back()->withInput(['msg' => $msg, 'msg_type' => $msg_type]);