【问题标题】:cakephp2.x component not workingcakephp2.x 组件不工作
【发布时间】:2012-05-18 05:18:06
【问题描述】:

我像这样创建我的组件

<?php
class UtilComponent extends Object
{
   function strip_and_clean( $id, $array) {
      $id = intval($id);
     if( $id < 0 || $id >= count($array) ) {
        $id = 0;
     }
      return $id;
   }
}

并像这样使用它

var $name = 'Books';
    var $uses = array();
    var $components = array('Util');
    public function index($id = 0){
        $this -> set('page_heading','Packt Book Store');
        $book=array(
            '0'=>array(
                'bookTitle'    =>'Object Oriented Programming with PHP5',
                'author'       =>'Hasin Hayder',
                'isbn'         => '1847192564',
            'releaseDate' => 'December 2007'
                ),
            '1'=>array(
                'bookTitle'    =>'Building Websites with Joomla! v1.0',
                'author'       =>'Hagen Graf',
                'isbn'         => '1904811949',
            'releaseDate' => 'March 2006'
                ),
            );
        $id = $this->Util->strip_and_clean( $id, $book);
        $this->set('book',$book[$id]);
        $this->pageTitle='Welcome to the Packt Book Store!';
    }

但我得到了这个错误

**call_user_func_array() 期望参数 1 是有效回调,类 'UtilComponent' 没有方法 'initialize' [CORE\Cake\Utility\ObjectCollection.php,第 130 行]**

【问题讨论】:

  • 尝试打印_r($id);给我看……
  • 尝试在行前打印 $id = $this->Util->strip_and_clean( $id, $book);在此之后也退出;展示给我们。

标签: cakephp components


【解决方案1】:

我认为组件应该扩展 Component 而不是 Object

class UtilComponent extends Component 
{

}

然后组件将继承initialize() 方法。

【讨论】:

  • 我们也可以使用 object .. class UtilComponent extends object
  • @Learner Component 类确实扩展了Object,但initialize() 方法在Component 中定义。这似乎是错误的结果。见api20.cakephp.org/class/component
  • 对,thanx.also the $this->set('book',$book[$id]); couse 另一个问题 Undefined variable:bookTitle,author, isbn,releaseDate.why?
  • 我使用的是 cake 1.3,我只使用对象。我可以在控制器中使用我的组件,请您指定初始化字...
  • 注意(8):未定义变量:bookTitle [APP\View\Books\index.ctp, line 3]
【解决方案2】:

遇到了类似的问题。

除了错误消息 ...没有方法 'initialize' ...,还有一条消息 ...没有方法 'startup' 。 ...

问题是我在Controller 目录而不是Controller/Component 中创建了组件文件。

【讨论】:

    【解决方案3】:

    我也面临同样的问题

    警告 (2):call_user_func_array() 期望参数 1 是有效回调,类 'ImageComponent' 没有方法 'initialize' [CORE\Cake\Utility\ObjectCollection.php,第 128 行]

    警告 (2):call_user_func_array() 期望参数 1 为有效回调,类 'ImageComponent' 没有方法 'beforeRender' [CORE\Cake\Utility\ObjectCollection.php,第 128 行]

    警告 (2):call_user_func_array() 期望参数 1 是有效回调,类 'ImageComponent' 没有方法 'shutdown' [CORE\Cake\Utility\ObjectCollection.php,第 128 行]

    解决办法是:

    然后将图像组件添加到控制器/组件中

    更改 类 ImageComponent 扩展对象 { ... }

    with class ImageComponent extends Component { ... }

    【讨论】:

      猜你喜欢
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-11
      • 2017-04-08
      相关资源
      最近更新 更多