How to extend or override CakePHP Core Helper functionality / methods

This: Cakephp Override HtmlHelper :: link asks a very similar question, but there were no answers. Perhaps now, with Cake 2, there will be.

I want to create a custom helper that is a subclass of the Pake Tool Helper. I want my new helper to override the Cake Paginator helper's "number" method, but I want it to inherit all other methods.

Is it possible to extend the main helpers in this way? Obviously, I don't want to: modify Cake Core; put unnecessary code in the superclass of AppHelper; or duplicate the entire main pagination helper in my new helper.

+4
source share
2 answers

Adding a Brelsnok solution, aliasing is the right way to do this. If you add this code to your AppController.php file, at any time when you use Paginator, it will use your extended class.

public $helpers = array( 'Paginator' => array('className' => 'PaginatorExt' ) ); 

Since the PaginatorExtHelper class already extends PaginatorHelper, the only overridden function is numbers . Any calls to other Paginator methods will be handled by the PaginatorHelper base class, just as if it were a vanilla Cake setup.

+11
source

Create the PaginatorExtHelper.php file in the View/Helper/ folder. It might look something like this. And instead of initiating $helpers = array('Paginator'); do $helpers = array('PaginatorExt');

 <?php App::uses('PaginatorHelper', 'View/Helper'); class PaginatorExtHelper extends PaginatorHelper { public function numbers($options = array()) { // logic here return $out; } } ?> 

Using only public function numbers() , you inherit other functions.

+5
source

All Articles