Mat*_*nya 4 php redirect flash-message laravel laravel-4
在我的控制器中,我有一个登录用户的功能.
如果登录成功,我可以简单地使用return Redirect::back().
当凭据不正确并且我想使用flash消息重定向时,我的问题就开始了.
我知道我可以将with方法链接到Redirect,但是这会将数据发送到特定视图,而不是登录HTML所在的布局.
我可以像这样加载一个视图:
$this->layout
->with('flash',$message)
->content = View::make('index');
Run Code Online (Sandbox Code Playgroud)
但我需要重定向回引用页面.
在将数据传递到布局时是否可以重定向?
Laravel Validator类处理得相当好....我通常这样做的方法是在我的布局/视图中添加条件...
{{ $errors->has('email') ? 'Invalid Email Address' : 'Condition is false. Can be left blank' }}
Run Code Online (Sandbox Code Playgroud)
如果有任何错误返回,这将显示一条消息.然后在验证过程中,您有......
$rules = array(check credentials and login here...);
$validation = Validator::make(Input::all(), $rules);
if ($validation->fails())
{
return Redirect::to('login')->with_errors($validation);
}
Run Code Online (Sandbox Code Playgroud)
这样......当你进入登录页面时,它会检查错误而不管是否提交,如果发现任何错误,它会显示你的消息.
编辑部分 用于处理Auth类..这在你看来......
@if (Session::has('login_errors'))
<span class="error">Username or password incorrect.</span>
@endif
Run Code Online (Sandbox Code Playgroud)
然后在你的身份......沿着这些方向......
$userdata = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
if ( Auth::attempt($userdata) )
{
// we are now logged in, go to home
return Redirect::to('home');
}
else
{
// auth failure! lets go back to the login
return Redirect::to('login')
->with('login_errors', true);
}
Run Code Online (Sandbox Code Playgroud)