Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Helpers in CakePHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 262
    Comment on it

    Helpers that are used in  CakePHP are associated with the  Presentation layers of application. Helpers basically contain presentational logic that are available to share between many views, elements, or layouts. We have different helpers used in CakePHP.

    • FormHelper
    • HtmlHelper
    • JsHelper
    • CacheHelper
    • NumberHelper
    • Paginator
    • RSS
    • SessionHelper
    • TextHelper

    Configuring and Using helpers

    You have to  enable helpers in CakePHP by making the controller aware of them. Each and every controller has a $helper property that basiacally lists the helpers to be made available in the view. If you want to enable a helper in your view, you have to add the name of the helper to the controller’s $helpers array

    class UsersController extends AppController {
        public $helpers = array('Form', 'Html', 'Js', 'Time');
    }

     

    Within an action you can also add helpers , so they will be accessible by only one action at a time  in the controlller This also manage the processing power for other actions in the controller, which are not using helper. 

     

    class UsersController extends AppController {
        public function hello() {
            $this->helpers[] = 'Time';
        }
        public function mix() {
            // The Time helper is not loaded here and thus not available
        }
    }

     

     To enable helper for all controllers, you should add the name of the helper to the $helpers array in /app/Controller/AppController.php

    class AppController extends Controller {
        public $helpers = array('Form', 'Html', 'Js', 'Time');
    }

     

    Options can be passed to helpers and these options can further used to set set attribute values or modify behavior of a helper:

    class AwesomeHelper extends AppHelper {
        public function __construct(View $view, $settings = array()) {
            parent::__construct($view, $settings);
            debug($settings);
        }
    }
    
    class AwesomeController extends AppController {
        public $helpers = array('Awesome' => array('option1' => 'value1'));
    }

     

    To create aliased helpers in your views, there is one setting to use i.e className option. This feature is useful when you want to replace $this->Html or another common Helper reference with a custom implementation:

    // app/Controller/PostsController.php
    class PostsController extends AppController {
        public $helpers = array(
            'Html' => array(
                'className' => 'MyHtml'
            )
        );
    }
    
    // app/View/Helper/MyHtmlHelper.php
    App::uses('HtmlHelper', 'View/Helper');
    class MyHtmlHelper extends HtmlHelper {
        // Add your code to override the core HtmlHelper
    }

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: