Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is the difference between Component, Helper and Behavior in CakePHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.59k
    Comment on it

    Hello Friends,

    Generally lots of programmer get confuse why we have three different things Component, Helper and Behavior in CAKEPHP while its seems to work same. Well the answer is yes all three items in CakePHP do the same kind of job, it extend current functionality of Cakephp. But Component, Helper and Behavior do the same job differently as below:

    Component :: This extend the functionality of Controller

    Behavior     :: This extend the functionality of Model

    Helper         :: This extend the functionality of View

    Example of Component :: In the below example we are using two component session and cookies. We use session to display success message of post deletion

    class PostsController extends AppController {
        public $components = array('Session', 'Cookie');
        // Here we are using two component in our controller
    
        public function delete() {
            if ($this->Post->delete($this->request->data('Post.id')) {
                // We use session component to show successful deletion message
                $this->Session->setFlash('Post deleted.');
                return $this->redirect(array('action' => 'index'));
            }
        }
    

     

    Example of Behavior :: In the below example we have changed the delete functionality of Delete operation

    class Post extends AppModel {
        $this->Post->Behaviors->load('softDelete');
    }
    
    // Now we can define behavior like this
    // models/behaviors/soft_delete.php
    class SoftDeleteBehavior extends ModelBehavior {
    
        // This will override the delete functionality
        function delete(&$Model, $id = null) {
            $Model->id = $id;
    
            // save the deleted field with value 1
            if ($Model->saveField('deleted', 1)) {
                return true;
            }
    
            return false;
        }
    }

    Example of Helper :: In the below example we use link helper to generate hyperlink

    class PostsController extends AppController {
        // Include Helper in your controller first
        public $helpers = array('Link');
    }
    
    //// Below code shows how to use this helper in view file
    <?php echo $this->Link->makeEdit('Change this Post', '/posts/edit/5'); ?>
    

    I hope you understand the basic difference between Component, Helper and Behavior in CAKEPHP

     

     

     

 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: