我有一个在 cake 2.x 上工作的裁剪工具组件和助手,但现在我需要在 cakephp 1.3 中的旧项目之一中使用这个裁剪工具。我该怎么做?
组件:
<?php
App::uses('Component', 'Controller');
class JcropComponent extends Component {
public $components = array('Session');
public $options = array(
'overwriteFile' => true,
'boxWidth' => '940'
);
/**
* Constructor
*/
public function __construct(ComponentCollection $collection, $options = array()) {
parent::__construct($collection,$options);
$this->options = array_merge($this->options, $options);
}
?>
helper
<?php
App::uses('AppHelper', 'View/Helper');
class JcropHelper extends AppHelper {
public $helpers = array('Html','Form','Js');
public $options = array(
'tooltip' => true,
'boxWidth' => '940'
);
public function __construct(View $view, $options = null) {
parent::__construct($view,$options);
$this->options = array_merge($this->options, $options);
}
?>
我尝试改成这样,可以显示图像,但是如何合并选项数组呢? 其中 __construct ($options = array())
<?php
class JcropComponent extends Component {
var $components = array('Session');
public $options = array(
'overwriteFile' => true,
'boxWidth' => '940'
);
//public function initialize(&$controller, $settings = array()) {
// $this->controller =& $controller;
//parent::__construct($collection,$options);
//$this->options = array_merge($this->options, $options);
//}
?>
<?php
class JcropHelper extends AppHelper {
var $helpers = array('Html','Form','Js');
public $options = array(
'tooltip' => true,
'boxWidth' => '940'
);
public function __construct($settings = null) {
//parent::__construct($view,$options);
//$this->options = array_merge($this->options, $options);
}
?>
最佳答案
1.3 中的组件不使用构造函数进行设置
您遇到的第一个重要问题是:组件接收设置的方式在主要版本之间发生了变化。
in 1.3 :
//Component(Collection) Class
$component->initialize($controller, $settings);
in 2.x :
//ComponentCollection class
new $componentClass(ComponentCollectionObject, $settings);
因此,让组件以 1.3 中相同的方式工作的方法是定义初始化方法。
助手有不同的构造函数
对助手进行了类似的更改:
In 1.3 :
//View class
new $helperCn($options);
In 2.x :
//HelperCollection class
new $helperClass(ViewObject, $settings);
在这种情况下,它应该更明显 - 任何重写的方法都应该具有与父类相同的方法签名。因此:更改帮助器以在构造函数中具有相同的预期参数
警告:扩展组件
在 1.3 中,组件不扩展 Component,而是扩展 Object。 Component 是 1.3 中充当集合的类,扩展它会导致意外和不期望的行为(即,它可能会导致意外的“随机”警告和 fatal error )。因此,您应该将组件类更改为与所有其他组件类似,扩展 Object(或者非常简单 - 不扩展组件)。
如果您在各种项目中使用和维护此类,那么最好将主要功能提取到一个独立的类中,并仅实现一个薄包装类(组件/行为)来与其交互。通过这种方式,无论所使用的 cake 版本或任何其他框架如何,都可以利用对主要功能所做的任何更改。
关于php - 如何将 cakephp 2.x 组件和助手转换为 1.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19776405/