Laravel Auth重定向到上一页

时间:2014-04-29 18:06:13

标签: php authentication laravel

我正在尝试使用Laravel 4创建一个类似应用程序的页面。当用户到达该站点时,应该提示他们登录。一旦用户登录,视图(不是URL)将切换并且用户将能够看到信息,就好像他们已经过身份验证一样。

我的HTML(如果已授权显示" Auth"在h1,如果没有,则显示登录表单)

<div class="container">
        @if(Auth::check())
            <h1>Auth</h1>
        @else
            {{ Form::open(array('url'=>'login', 'method'=>'post')) }}
            <div class="row">
                <div class="col-xs-12">
                    <div class="form-group">
                        {{ Form::label('email', 'Email Address') }}
                        {{ Form::text('email', Input::old('email'), array('class'=>'form-control', 'placeholder'=>'example@test.com')) }}
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-12">
                    <div class="form-group">
                        {{ Form::label('password', 'Password') }}
                        {{ Form::password('password', array('class'=>'form-control')) }}
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-12">
                    {{ Form::submit('Log In', array('class'=>'btn btn-primary pull-right')) }}
                </div>
            </div>
            {{ Form::close() }}
        @endif
    </div>

控制器

class SiteController extends BaseController {

    public function getIndex()
    {
        return View::make('index');
    }

    public function postLogin() {
        $email = Input::get('email');
        $password = Input::get('password');
        if (Auth::attempt(array('email'=>$email, 'password'=>$password)))
        {
            return Redirect::route('index');
        }
    }

}

我的用户模型是Laravel 4附带的默认模型。截至目前,我正在传递Auth::attempt并获取return Redirect::route('index');,但@if(Auth::check())似乎没有被射击。相反,它继续向我显示登录表单。我在这里做错了吗?

1 个答案:

答案 0 :(得分:0)

我在这里看不到任何错误,但您需要确定发生了什么,看起来您的身份验证会话并不坚持,但请确保您可以:

<?php

class SiteController extends BaseController {

    public function getIndex()
    {
        Log::info('index - authed: '. Auth::check() ? 'yes' : 'no');

        return View::make('index');
    }

    public function postLogin() {
        $email = Input::get('email');

        $password = Input::get('password');

        if (Auth::attempt(array('email'=>$email, 'password'=>$password)))
        {
            Log::info('postLogin - attempt successful');

            return Redirect::route('index');
        }

        Log::info('postLogin - error on attempt');
    }

}

然后查看您的日志:

php artisan tail
相关问题