• 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to extend the Controller core class

#4
1) Create a new folder Base ( ./application/Controllers/Base )

2) Create the BaseController.php ( ./application/Controllers/Base/BaseController.php )

PHP Code:
<?php namespace App\Controllers\Base;

/**
 * Class BaseController
 *
 * @package App\Controllers\Base
 */
class BaseController extends \CodeIgniter\Controller
{

    
/**
     * Class variables - public, private, protected and static.
     * -----------------------------------------------------------------------
     */

    /**
     * @var  array
     */
    
protected $helpers = [];

    
/**
     * __construct ()
     * -----------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     *
     */
    
public function __construct(...$params)
    {
        
parent::__construct(...$params);

        
// load helpers - helper(['url', 'form']);
        
helper(['url']);
    }

  // End of BaseController Class.

/**
 * ---------------------------------------------------------------------------
 * Filename: BaseController.php
 * Location: ./application/Controllers/Base/BaseController.php
 * ---------------------------------------------------------------------------
 */ 

3) Create the AdminController.php ( ./application/Controllers/Base/AdminController.php )

PHP Code:
<?php namespace App\Controllers\Base;

/**
 * Class AdminController
 *
 * @package App\Controllers\Base
 */
class AdminController extends BaseController
{

    
/**
     * Class variables - public, private, protected and static.
     * -----------------------------------------------------------------------
     */


    /**
     * __construct ()
     * --------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     *
     */
    
public function __construct (...$params)
    {
        
parent::__construct(...$params);
        
    }

  // End of AdminController Class.

/**
 * ---------------------------------------------------------------------------
 * Filename: AdminController.php
 * Location: ./application/Controllers/Base/AdminController.php
 * ---------------------------------------------------------------------------
 */ 

4) Create the PublicController.php ( ./application/Controllers/Base/PublicController.php )

PHP Code:
<?php namespace App\Controllers\Base;

/**
 * Class PublicController
 *
 * @package App\Controllers\Base
 */
class PublicController extends BaseController
{

    
/**
     * Class variables - public, private, protected and static.
     * -----------------------------------------------------------------------
     */


    /**
     * __construct ()
     * -----------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     *
     */
    
public function __construct(...$params)
    {
        
parent::__construct(...$params);
        
    }

  // End of PublicController Class.

/**
 * ---------------------------------------------------------------------------
 * Filename: PublicController.php
 * Location: ./application/Controllers/Base/PublicController.php
 * ---------------------------------------------------------------------------
 */ 

5) Extending the other Controllers ( ./application/Controllers/Home.php )

PHP Code:
<?php namespace App\Controllers;

// use App\Controllers\Base\AdminController;
use App\Controllers\Base\PublicController;

// class Home extends AdminController
class Home extends PublicController
{
    
/**
     * Class variables - public, private, protected and static.
     * -----------------------------------------------------------------------
     */


    /**
     * __construct ()
     * --------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     *
     */
    
public function __construct (...$params)
    {
        
parent::__construct(...$params);

    }

    public function 
index()
    {
        return 
view('welcome_message');
    }

    
//--------------------------------------------------------------------



That's all there is to it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Messages In This Thread
How to extend the Controller core class - by Edel - 07-22-2017, 03:35 PM
RE: How to extend the Controller core class - by InsiteFX - 07-23-2017, 04:18 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.