经验丰富的程序员,但对 CakePHP 2.1 很陌生,我花了一天的时间努力让自定义 Helper 按照手册在 View 中工作:http: //book.cakephp.org/2.0/en/views/helpers.html
我无法找到答案,非常感谢任何 Cake 专业知识。
我在 app/Helper/EntriesHelper.php 中的帮助文件:
App::uses('AppHelper', 'View/Helper');
class EntriesHelper extends AppHelper {
public function __construct(View $view, $settings = array()) {
parent::__construct($view, $settings);
}
public function spanWrapper($content) {
if(substr($content,0,1) == "@") {
return "<span class='label label-warning'>$content</span>";
}
else if(substr($content,0,1) == "#") {
return "<span class='label label-default'>$content</span>";
}
else if (substr($content,0,4) == "http" || substr($content,0,3) == "www") {
return "<span class='label'>$content</span>";
}
return $content;
}
}
我在 app/Controller/EntriesController 中的控制器:
App::uses('AppController', 'Controller');
class EntriesController extends AppController {
public $helpers = array('Form', 'Html', 'Js', 'Time');
#public $components = array('RequestHandler');
#public $viewClass = 'Json';
public function index() {
$helpers[] = 'spanWrapper';
$this->Entry->recursive = 1;
$this->set('entries', $this->paginate());
#$this->set('_serialize', array('entries'));
}
}
但是来自我的视图的呼叫失败:
$this->Entries->spanWrapper($entry['Entry']['title']);
出现错误:
Notice (8): Undefined property: View::$Entries [CORE/Cake/View/View.php, line 806]
Fatal error: Call to a member function spanWrapper() on a non-object in <path removed>/app/View/Entries/index.ctp on line 35
因此,未定义属性的通知可能会导致致命错误......但是为什么会这样,如果它是按照食谱实现的呢?
达伦