Cakephp Override HtmlHelper::link

前端 未结 7 1350
离开以前
离开以前 2021-01-06 18:42

I want to setup HtmlHelper::link() method so the default options array have escape = false.

How can I achieve this without changing the core class?

OBS: I al

7条回答
  •  隐瞒了意图╮
    2021-01-06 19:03

    another good practice that I think cakephp should implement that you also can implement is a simple Factory Pattern Helper. The following should be consider just psuedo not real code.

    $this->Factory->getHelper('Html')->link();
    

    instead of

    $this->Html->link();
    

    take the following for example

    class FactoryHelper extends  Helper {
        public function getHelper($name) {
            if(Configure::read('Overrides.{$name}')) {
                return $this->{Configure::read('Overrides.{$name}')};
            }
            return (isset($this->{$name})?$this->{$name}:false);
        }
    
    }
    
    //Bootstrap is where you will set all your overrides
    Configure::write('Overrides',array(
        'Html'=>'NewHtml'
    ));
    

    //so now when you want to override any helper you can

    So now in the bootstrap that you set to override Html Helper. Throughout your entire site, your new 'NewHtml' helper will be called instead of the traditional helper.

提交回复
热议问题