Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop 带有Cartalyst\Sentry的Laravel 4,提供通用接口_Oop_Inheritance_Laravel_Laravel 4 - Fatal编程技术网

Oop 带有Cartalyst\Sentry的Laravel 4,提供通用接口

Oop 带有Cartalyst\Sentry的Laravel 4,提供通用接口,oop,inheritance,laravel,laravel-4,Oop,Inheritance,Laravel,Laravel 4,我想在我的项目中使用Sentry进行用户身份验证,我已经设置好了 我的想法是提供一种适配器,以便以后更改实现。由于我们可能计划稍后更改为LDAP,我想调用我自己的类上的所有函数 我建立了一个新的Facade和一个名为UMS的类,用户管理系统的缩写,然后我想调用UMS::check而不是Sentry::check 我如何正确地做到这一点?我想做的只是: <?php namespace Vendor\Dashboard; class UMS extends \Sentry { pu

我想在我的项目中使用Sentry进行用户身份验证,我已经设置好了

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

我建立了一个新的Facade和一个名为UMS的类,用户管理系统的缩写,然后我想调用UMS::check而不是Sentry::check

我如何正确地做到这一点?我想做的只是:

<?php namespace Vendor\Dashboard;

class UMS extends \Sentry {

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

}

?>

什么到目前为止不起作用,因为我得到了一个cannotcall构造函数异常。我有点困惑,因为我觉得Laravel有点滥用Facade模式来启用后期绑定。通常一个门面仅指一个类。我只是想稍后更改UMS背后的实现,因此不在代码中调用Sentry。但我应该能像往常一样在岗哨上调用所有方法。也许解决方案是显而易见的

这是我目前的解决方案,不知道是否有更好的解决方案,但它是有效的,只是通过我自己的类调用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();
    }

}

?>

您还可以将应用程序的Sentry实例注入到类中。以下内容适用于注册功能中的服务提供商,但与此类似:

$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();
    }

}

并根据需要添加函数。

在Laravel 4中,要正确扩展类并创建新外观,必须:

基于另一个类创建新类:

<?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?

}
将您的外观添加到app/config/app.php中的别名列表中:

'providers' => array(
    ...
    'AntonioRibeiro\UserManagementSystem\UMSServiceProvider',
),
'aliases' => array(
    ...
    'UMS'             => 'AntonioRibeiro\UserManagementSystem\UMSFacade',
),
更新自动加载的类:

composer du --optimze
并使用它:

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

或者我应该简单地通过LDAP更改UserProvider吗?据我所知,您希望在创建适配器时使用依赖项注入,而不是继承。因此,我会将所需的Sentry库/类注入到新类中,然后在适配器中调用所需的任何方法。是的,这是我到目前为止所做的,因此我从自己的类调用Sentry方法似乎更好,我将很快尝试实现它!谢谢,这里没有适合他们的地方。在app/上创建一个文件夹,将它们添加到那里,将该文件夹添加到composer.json/autoload/classmap部分,然后运行composer dump-autoload。此解决方案有问题。
composer du --optimze
var_dump( UMS::check() );
var_dump( UMS::getUser() );