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

 王强丫ES 发布于 2023-02-07 17:14

假设我有这个代码用于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 个回答
  • 在哨兵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.';
            }
        }
    
    }
    

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

    2023-02-07 17:15 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有