最近我已经从Codeigniter改为Laravel,除了遇到了Session :: flash的问题,一切顺利.
原文链接:https://www.f2er.com/laravel/139826.html当我创建新用户时,我会获得成功消息,但是会持续2个请求,即使我没有通过验证器:
我的代码在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',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','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()); }
在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
在master.blade:
@if(Session::has('message')) <p class="alert {{ Session::get('alert-class','alert-info') }} alert-dismissable"> {{ Session::get('message') }}</p> @endif
也许我不是孤独的这个问题,here是另一个未回答的问题.
更新
对于将来面临这个问题的人来说:
不重新启动会话数据.
我的代码现在看起来像这样:
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','alert-success'); } else { Session::flash('message',Helpers::formatErrors($validator->messages()->all())); Session::flash('alert-class','alert-danger'); } return Redirect::action('UsersController@getCreateUser'); }