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

#1
Hello, I am implementing in CI4 some utilities that I had done using CI3. The first I need es extend the controller core class for add it new functionalities. I read the documentation but always CI returns an error. So , somebody can tell me how do it.?
Reply

#2
Try this:

https://john-betong.tk/test/done/extend-...ler-my_lib
Reply

#3
Showing code you tried and telling the actual error you received is the best way to get a good answer. That said - extending the Controller class is pretty simple - you're just extending a normal PHP class, no special MY_ prefixes needed or anything like that.

1. Make a new BaseController class/file. Make sure it is namespaced so the autoloader can find it.

PHP Code:
<?php namespace App\Controllers;

class 
BaseController extends \CodeIgniter\Controller 
{
    public function 
__construct(...$params)
    {
        
parent::__construct(...$params);
    }


2. Have all of your new controllers extend BaseController instead of CodeIgniter\Controller.

Note: you don't need to have the constructor shown here unless you need additional tasks in the constructor. I just showed it here to remind people about the parameter packing/un-packing available in PHP 7.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#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

#5
Thanks for your replies, It's all clear to me now. I corrected my errors and successfully extended the class controllers.
Reply

#6
My thanks also go to Kilishan and InsiteFX. I've been trying to do this when converting my CI 3.x code to Ver. 4.
CI 3.1 Kubuntu 19.04 Apache 5.x&nbsp; Mysql 5.x PHP 5.x PHP 7.x
Remember: Obfuscation is a bad thing.
Clarity is desirable over Brevity every time.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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