0

假设我有此代码进行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)没有显示任何内容。

4

1 回答 1

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();
}
于 2013-12-29T04:49:26.143 回答