自定义验证对象中的自定义组件

时间:2012-07-20 16:17:29

标签: cakephp

如何在自定义身份验证对象中使用组件(自定义或基础,如Session)? (位于Controller/Components/Auth)??

我尝试了public $components = array('Session', 'name of my custom component');,但它不起作用。

...谢谢

2 个答案:

答案 0 :(得分:1)

由于我自己正在寻找一种方法,并且我发现了这个问题,我将分享我的发现。

我看了一下BaseAuthenticate :: __ construct(),看起来AuthComponent将控制器的ComponentCollection发送到每个Autehntication对象。我试着做了

$this->Session = $this->_Collection->__get('Session');
$this->Session->write('foo', 'bar');

它工作但只是因为我已经在我的AppController中加载SessionComponent。 ComponentCollection :: __ get()仅返回已加载的组件。您可能希望查看ComponentCollection类及其父对象ObjectCollection,以获取有关如何使用组件的更多详细信息。

这是我在身份验证类中所做的:

/**
 * Constructor
 *
 * @param ComponentCollection $collection The Component collection used on this request.
 * @param array $settings Array of settings to use.
 */
public function __construct(ComponentCollection $collection, $settings) {
    $settings = array_merge($this->settings, $settings);
    parent::__construct($collection, $settings);
    $this->http = new HttpSocket();
    $this->_initComponentCollection();
}

/**
 * Initializes all the loaded Components for the Authentication Object.
 * Attaches a reference of each component to the Authentication Object.
 *
 * @return void
 */
protected function _initComponentCollection() {
    $components = $this->_Collection->loaded();
    if (!empty($components)) {
        foreach ($components as $name) {
            $this->{$name} = $this->_Collection->__get($name);
        }
    }
}

这仅使用已加载的组件,因为我不想加载所有组件。我将用cakephp打开一张票,看看是否可以在BaseAuthenticate类中添加类似的代码段。

答案 1 :(得分:-1)

根据CakeBook:

// Pass settings in $components array
public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'name of custom authentication object'
        )
    )
);

Auth组件的authenticate属性是您声明要使用的身份验证对象的位置,无论是表单,基本,摘要还是自定义对象。