Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to inherit a controller in another controllers in cakephp

    • 0
    • 2
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 417
    Comment on it

    Hi reader's today we will discuss "How to inherit a controller in another controllers in cakephp".
    In cake php controllers are nothing more than php classes, You can inherit a controller to another just like a hierarchy of inheritance. Let's have a example, here we are creating a controller "SupersController.php", and a drived controller "SubsController.php".
    Here is SupersController.php Controller.

    <?php
    App::uses('AppController', 'Controller');
    /**
     * Supers Controller
     * Authur:Jaydeep
     */
    class SupersController extends AppController {
        function className()
        {
            return "Hi i am SupersController";
        }
    }
    


    Now to inherit the "SupersController", you need to give the class definition to drive class as App::import('Controller', 'Supers');, than define the class properly like this "class SubsController extends SupersController {}". Now to call Supers class function create a object of "SupersController" and call its function className(). Here one thing we must take note is that at a time a controller can extend only one controller class.
    Here is SubsController.php Controller.

    <?php
    App::import('Controller', 'Supers');
    /**
     * Subs Controller
     *
     */
     // extends SupersController note no need to inherit AppController
    class SubsController extends SupersController { 
        function getClassName() 
        {
            //create a object of Super class.
            $obj = new SupersController;
            //call super class function
            $obj->className();
        }
    }
    

    **output**:
    Hi i am SupersController.

 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: