donglizhan7848 2013-12-29 04:30
浏览 29
已采纳

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

Suppose I have this code for Sentry authentication:

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

I want to try to remove all the try and catch (I have my reason, it's multilevel authentication, I'm trying to shorten my code). So I tried this to check if whether authentication failed:

$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! */
}

The is_null won't work, the code won't continue. Any suggestions or solutions?

EDIT

Using Chrome's console, var_dump($user) did not show anything.

  • 写回答

1条回答 默认 最新

  • dqc2017 2013-12-29 04:49
    关注

    In Sentry 2 you can't, Sentry 3 will let you do it, but it still not finished. So you best bet is to create a service and a Facade for it:

    Create your service:

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

    Create a ServiceProvider to boot it up:

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

    A Facade:

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

    And now you just have to add it all to ServiceProviders

    'Acme\Services\Authentication\ServiceProvider',
    

    and Aliases in your config/app.php:

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

    And use it:

    $credentials = array(
                    'email'    => Input::get('email'),
                    'password' => Input::get('password'),
                );
    
    if ( ! $user = Authentication::authenticate($credentials))
    {
        echo Authentication::getError();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真