Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend or override functionality/methods of a CakePHP Core Helper

Tags:

cakephp

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

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

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

like image 580
joshua.paling Avatar asked Jul 25 '12 12:07

joshua.paling


1 Answers

Adding onto Brelsnok's solution, aliasing is the right way to do it. If you add this code to your AppController.php file, anytime that you use the Paginator, it will use your extended class.

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

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

like image 181
Derek Perkins Avatar answered Sep 20 '22 19:09

Derek Perkins