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

use*_*495 0 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.';
        }
Run Code Online (Sandbox Code Playgroud)

我想尝试删除所有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! */
}
Run Code Online (Sandbox Code Playgroud)

is_null将无法正常工作,代码将无法继续.任何建议或解决方案?

编辑

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

Ant*_*iro 5

在哨兵2你不能,哨兵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.';
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

创建一个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');
    }

}
Run Code Online (Sandbox Code Playgroud)

门面:

<?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'; }

}
Run Code Online (Sandbox Code Playgroud)

现在,您只需将其全部添加到ServiceProviders即可

'Acme\Services\Authentication\ServiceProvider',
Run Code Online (Sandbox Code Playgroud)

和你的别名config/app.php:

'Authentication' => 'Acme\Services\Authentication\Facade',
Run Code Online (Sandbox Code Playgroud)

并使用它:

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

if ( ! $user = Authentication::authenticate($credentials))
{
    echo Authentication::getError();
}
Run Code Online (Sandbox Code Playgroud)