首页 文章

Laravel 4与Cartalyst \ Sentry一起提供通用接口

提问于
浏览
0

我想在我的项目中使用Sentry进行用户身份验证,我已将其设置为正常工作 .

我的想法是提供一种适配器来改变以后的实现 . 由于我们可能计划稍后更改为LDAP,我想调用我自己的类中的所有函数 .

我设置了一个新的Facade和一个名为 UMS 的类, User Management System 的缩写,然后我想调用 UMS::check() 而不是 Sentry::check()

我该怎么做呢?我想到了这样做:

<?php namespace Vendor\Dashboard;

class UMS extends \Sentry {

    public function __construct() {
       parent::__construct();
    }

}

?>

到目前为止什么不起作用,因为我得到了一个 Cannot call constructor 例外 . 我有点迷惑,因为我觉得Laravel有点滥用Facade模式来启用后期绑定 . 通常一个立面仅指一个类 . 我只想稍后更改 UMS 后面的实现,因此不要在我的代码中调用 Sentry . 但我应该像往常一样在 Sentry 上调用所有方法 . 也许解决方案很明显

3 回答

  • 0

    在Laravel 4中,要正确扩展Class并创建新Facade,您必须:

    基于另一个创建新类:

    <?php namespace AntonioRibeiro\UserManagementSystem;
    
    class UMS extends \Cartalyst\Sentry\Sentry {
    
        // For now your UMS class is identical to Sentry, so you must have nothing here, right?
    
    }
    

    创建一个ServiceProvider,它将实例化您的类:

    <?php namespace AntonioRibeiro\UserManagementSystem;
    
    use Illuminate\Support\ServiceProvider;
    
    class UMSServiceProvider extends ServiceProvider {
    
        protected $defer = true;
    
        public function register()
        {
            $this->app['ums'] = $this->app->share(function($app)
            {
                // Once the authentication service has actually been requested by the developer
                // we will set a variable in the application indicating such. This helps us
                // know that we need to set any queued cookies in the after event later.
                $app['sentry.loaded'] = true;
    
                return new UMS(
                    $app['sentry.user'],
                    $app['sentry.group'],
                    $app['sentry.throttle'],
                    $app['sentry.session'],
                    $app['sentry.cookie'],
                    $app['request']->getClientIp()
                );
            });
        }
    
        public function provides()
        {
            return array('ums');
        }
    
    }
    

    创建Facade:

    <?php namespace AntonioRibeiro\UserManagementSystem;
    
    use Illuminate\Support\Facades\Facade as IlluminateFacade;
    
    class UMSFacade extends IlluminateFacade {
    
        protected static function getFacadeAccessor() { return 'ums'; }
    
    }
    

    将您的服务提供商添加到app / config / app.php中的列表:

    'providers' => array(
        ...
        'AntonioRibeiro\UserManagementSystem\UMSServiceProvider',
    ),
    

    将Facade添加到app / config / app.php中的别名列表中:

    'aliases' => array(
        ...
        'UMS'             => 'AntonioRibeiro\UserManagementSystem\UMSFacade',
    ),
    

    更新自动加载的类:

    composer du --optimze
    

    并使用它:

    var_dump( UMS::check() );
    var_dump( UMS::getUser() );
    
  • 3

    这是我现在的解决方案,不知道是否有更好的解决方案,但它可以工作,只需通过我自己的类调用Sentry上的函数

    <?php namespace Vendor\Dashboard;
    
    use Cartalyst\Sentry\Facades\Laravel\Sentry as Sentry;
    
    class UMS {
    
        public function __construct() {
    
        }
    
        public static function check() {
            return Sentry::check();
        }
    
        public static function authenticate($credentials, $rememberme) {
            return Sentry::authenticate($credentials, $rememberme);
        }
    
        public static function logout () {
            return Sentry::logout();
        }
    
        public static function getUser() {
            return Sentry::getUser();
        }
    
        public static function getUserProvider() {
            return Sentry::getUserProvider();
        }
    
    }
    
    ?>
    
  • 4

    您还可以将应用程序的Sentry实例注入您的 class . 以下是服务提供商(在注册函数中),但它类似:

    $this->app['myPackage.user'] = $this->app->share(function($app)
        {
            return new myClass($app['sentry']);
        });
    
    $this->app['myAlias'] = $this->app->share(function($app)
        {
    
            return new myPackage($app['myPackage.user']);
        });
    

    然后,在myClass中:

    class myClass {
    
        protected $sentry;
    
        function __construct($sentry){
            $this->sentry = $sentry;
        }
    
        public function getUser() {
            return $this->sentry->getUser();
        }
    
    }
    

    并根据需要添加您的功能 .

相关问题