Laravel Session Flash 持续 2 个请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24579580/
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 Session Flash persists for 2 requests
提问by Walid Ammar
Recently I have changed to Laravel from Codeigniter, everything was going fine except I encountered a problem with Session::flash
.
最近我从 Codeigniter 改用 Laravel,一切都很顺利,除了我遇到了Session::flash
.
when I create new user I get success message but It will persist for 2 requests, even I didn't pass the validator:
当我创建新用户时,我收到成功消息,但它会持续 2 个请求,即使我没有通过验证器:
my code in UsersController
:
我的代码UsersController
:
function getCreateUser(){
$config = array(
'pageName' => 'createUser',
'pageTitle' => 'Create User',
'formUrl' => action('UsersController@postCreateUser'),
'modelFields' => array(
array('db_name' => 'employee_id', 'text' => 'Employee Id', 'mandatory' => TRUE),
array('db_name' => 'full_name', 'text' => 'Full Name', 'mandatory' => TRUE),
array('db_name' => 'email', 'text' => 'Email', 'mandatory' => FALSE),
array('db_name' => 'password', 'text' => 'Password','value' => '12345', 'mandatory' => TRUE)
),
'submit_text' => 'Create'
);
return View::make('layouts.form', $config);
}
function postCreateUser(){
$config = array(
'pageName' => 'createUser',
'pageTitle' => 'Create User',
'formUrl' => action('UsersController@postCreateUser'),
'modelFields' => array(
array('db_name' => 'employee_id', 'text' => 'Employee Id', 'mandatory' => TRUE),
array('db_name' => 'full_name', 'text' => 'Full Name', 'mandatory' => TRUE),
array('db_name' => 'email', 'text' => 'Email', 'mandatory' => FALSE),
array('db_name' => 'password', 'text' => 'Password','value' => '12345', 'mandatory' => TRUE)
),
'submit_text' => 'Create'
);
$validator = User::validate(Input::all());
if($validator->passes()){
$user = new User(Input::all());
$user->password = Hash::make(Input::get('password'));
$user->Company_id = '1';
$user->save();
Session::flash('message', 'User Created Successfully!');
Session::flash('alert-class', 'alert-success');
return View::make('layouts.form', $config);
}
return View::make('layouts.form', $config)->withErrors($validator->messages());
}
in form.blade
:
在form.blade
:
@if ( $errors->count() > 0 )
<div class="alert alert-danger">
<p>The following errors have occurred:</p>
<ul>
@foreach( $errors->all() as $message )
<li>{{ $message }}</li>
@endforeach
</ul>
</div>
@endif
in master.blade
:
在master.blade
:
@if(Session::has('message'))
<p class="alert {{ Session::get('alert-class', 'alert-info') }} alert-dismissable"> {{ Session::get('message') }}</p>
@endif
Maybe I'm not alone with this issue, hereis another unanswered question.
也许我并不孤单这个问题,这里是另一个悬而未决的问题。
Update
更新
For anyone in future facing this problem: Never flash session data without redirecting.
对于将来遇到此问题的任何人: 切勿在不重定向的情况下刷新会话数据。
My code now looks like this:
我的代码现在看起来像这样:
function postCreateUser(){
$validator = User::validate(Input::all());
if($validator->passes()){
$user = new User(Input::all());
$user->password = Hash::make(Input::get('password'));
$user->Company_id = '1';
$user->save();
Session::flash('message', 'User Created Successfully!');
Session::flash('alert-class', 'alert-success');
} else {
Session::flash('message', Helpers::formatErrors($validator->messages()->all()));
Session::flash('alert-class', 'alert-danger');
}
return Redirect::action('UsersController@getCreateUser');
}
回答by Drew
You are Flashing session data and creating a view instead of redirecting, meaning the message will Flash for this request and for the next one, showing twice.
您正在闪烁会话数据并创建视图而不是重定向,这意味着消息将针对此请求和下一个请求闪烁,显示两次。
If you want to show the message on the current request without redirecting, I would suggest providing the errors to your View::make
instead of trying to Flash the messages. If you MUST Flash the message on the current request, then you will need to Session::forget('key')
or Session::flush()
after your view.
如果您想在不重定向的情况下显示当前请求中的消息,我建议您将错误提供给您,View::make
而不是尝试闪烁消息。如果您必须在当前请求上闪烁消息,那么您将需要Session::forget('key')
或Session::flush()
在您的视图之后。
回答by aross
The following seems to be available from version 5.1 onward. Although the method is notmentioned on Laravel's documentation page about sessions.
从 5.1 版开始,以下内容似乎可用。尽管Laravel 的关于session 的文档页面上没有提到该方法。
session()->now()
This is the same as flash, except it won't persist to the next request.
这与 flash 相同,只是它不会持续到下一个请求。
回答by Mwirabua Tim
As @Drew said earlier
正如@Drew 之前所说
You are Flashing session data and creating a view instead of redirecting, meaning the message will Flash for this request and for the next one, showing twice.
您正在闪烁会话数据并创建视图而不是重定向,这意味着消息将针对此请求和下一个请求闪烁,显示两次。
An easy way to flash a message once when you are creating a view is:
创建视图时闪烁一次消息的简单方法是:
Session::flash($key, $value);
Session::push('flash.old', $key);
Happy coding!
快乐编码!
回答by Neil Telford
I had a similar problem, but I couldn't use Return::redirct, as I was using Ajax to to post to and from a within a set of Tabs.
我遇到了类似的问题,但我无法使用 Return::redirct,因为我使用 Ajax 来向一组选项卡中的某个选项卡发布信息。
Therefore, I was calling
因此,我打电话
Input::flashExcept('_token');
only if the validation failed and returning a view with the old input. Assuming validation passed and I wanted to run some functions and create a new view based on new data, I would call:
仅当验证失败并返回带有旧输入的视图时。假设验证通过并且我想运行一些函数并基于新数据创建一个新视图,我会调用:
Session::forget('_old_input');
I would put this before my final View::make
我会把这个放在我的决赛之前 View::make
Hope this helps (or makes sense)...
希望这有帮助(或有意义)...
回答by Tjeu Moonen
create:
创建:
$request->session()->flash('status', 'Task was successful!');
delete:
删除:
$request->session()->forget('status');
回答by AMIB
A good method to repopulate form with old data:
用旧数据重新填充表单的好方法:
Controller:
控制器:
View::make( 'view.name' )->withOldFormData( Input::all() );
View:
看法:
{{ Form::model( $old_form_data ) }}