Laravel 4 Sentry 2身份验证检查不使用`catch`?

时间:2013-12-29 04:30:01

标签: php laravel laravel-4 cartalyst-sentry

假设我有Sentry身份验证的代码:

try {
            $credentials = array(
                'email'    => Input::get('email'),
                'password' => Input::get('password'),
            );

            $user = Sentry::authenticate($credentials, false);

        }
        catch (Cartalyst\Sentry\Users\LoginRequiredException $e) {
            echo 'Login field is required.';
        }
        catch (Cartalyst\Sentry\Users\PasswordRequiredException $e) {
            echo 'Password field is required.';
        }
        catch (Cartalyst\Sentry\Users\WrongPasswordException $e) {
            echo 'Wrong password, try again.';
        }
        catch (Cartalyst\Sentry\Users\UserNotFoundException $e) {
            echo 'User was not found.';
        }
        catch (Cartalyst\Sentry\Users\UserNotActivatedException $e) {
            echo 'User is not activated.';
        }

我想尝试删除所有trycatch(我有我的理由,它是多级身份验证,我试图缩短我的代码)。所以我试着检查验证是否失败:

$credentials = array(
                'email'    => Input::get('email'),
                'password' => Input::get('password'),
            );

            $user = Sentry::authenticate($credentials, false);

            if (is_null($user)) {
                /* error logic here */
            }
else{
   /* login success! */
}

is_null无法正常工作,代码不会继续。有什么建议或解决方案吗?

修改

使用Chrome的控制台,var_dump($user)没有显示任何内容。

1 个答案:

答案 0 :(得分:5)

在Sentry 2你不能,Sentry 3会让你这样做,但它还没有完成。所以你最好的办法是为它创建一个服务和一个Facade:

创建您的服务:

<?php namespace Acme\Services\Authentication;

use Cartalyst\Sentry\Sentry;

class Service {

    private $this->error;

    public function __construct(Sentry $sentry)
    {
        $this->sentry = $sentry;    
    }

    public function getError()
    {
        return $this->error;
    }

    public function authenticate($credentials, $remember = true)

        try {
            return Sentry::authenticate($credentials, $remember);
        }
        catch (Cartalyst\Sentry\Users\LoginRequiredException $e) {
            $this->error = 'Login field is required.';
        }
        catch (Cartalyst\Sentry\Users\PasswordRequiredException $e) {
            $this->error = 'Password field is required.';
        }
        catch (Cartalyst\Sentry\Users\WrongPasswordException $e) {
            $this->error = 'Wrong password, try again.';
        }
        catch (Cartalyst\Sentry\Users\UserNotFoundException $e) {
            $this->error = 'User was not found.';
        }
        catch (Cartalyst\Sentry\Users\UserNotActivatedException $e) {
            $this->error = 'User is not activated.';
        }
    }

}

创建一个ServiceProvider来启动它:

<?php namespace Acme\Services\Authentication;

use Illuminate\Support\ServiceProvider as  IlluminateServiceProvider;
use Acme\Services\Authentication\Service as Authentication;

class ServiceProvider extends IlluminateServiceProvider {

    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = true;

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('authentication', function($app) {

            return new Authentication($app->make('sentry'));

        });
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return array('authentication');
    }

}

门面:

<?php namespace Acme\Services\Authentication;

use Illuminate\Support\Facades\Facade as IlluminateFacade;

class Facade extends IlluminateFacade {

    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor() { return 'authentication'; }

}

现在你只需要将它全部添加到ServiceProviders

'Acme\Services\Authentication\ServiceProvider',

config/app.php中的别名:

'Authentication' => 'Acme\Services\Authentication\Facade',

并使用它:

$credentials = array(
                'email'    => Input::get('email'),
                'password' => Input::get('password'),
            );

if ( ! $user = Authentication::authenticate($credentials))
{
    echo Authentication::getError();
}