Welcome Guest, Not a member yet? Register   Sign In
Loading core controller as well as composer libraries
#1

Hi there

Some of my site's controllers extend `core/Api_controller.php` like this:

Code:
class Basket extends Api_Controller {

Api_controller, in turn, extends `core/MY_Controller` which extends `CI_Controller`


In order to make this work I had to add the following to the bottom of my config.php:

Code:
function __autoload($class)
{
   if (strpos($class, 'CI_') !== 0)
   {
       if (file_exists($file = APPPATH . 'core/' . $class . '.php'))
       {
           include $file;
       }
       elseif (file_exists($file = APPPATH . 'libraries/' . $class . '.php'))
       {
           include $file;
       }
   }
}


This worked fine... until I wanted to load a 3rd party library. I enabled `composer_autoload` in my config.php:

Code:
$config['composer_autoload'] = '../vendor/autoload.php';

'vendor' is outside my application directory. I have verified that this autoload file is being loaded.



But now when I load one of my API controllers I get an error:

Code:
Class 'Api_Controller' not found in [absolute path]/deploy/application/controllers/api/Basket.php on line 12


I assume this is because my autoload paths are being managed by the Composer autoloader.


How can I load custom core controllers as well as using 3rd party libraries in my vendor directory?
Reply
#2

You do not need to add that to the bottom of your config file.

Just put them all in to one MY_Controller file like so.

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * -----------------------------------------------------------------------
 * Editor   : PhpStorm
 * Date     : 1/12/2018
 * Time     : 7:40 AM
 * Authors  : Raymond L King Sr.
 * -----------------------------------------------------------------------
 *
 * Class        MY_Controller
 *
 * @project     ci3admin
 * @author      Raymond L King Sr.
 * @link        http://www.procoversfx.com
 * @copyright   Copyright (c) 2009 - 2018 Custom Software Designers, LLC.
 * @license     http://www.procoversfx.com/license
 * -----------------------------------------------------------------------
 */

/**
 * Class BaseController
 *
 * Make sure your save it as
 * ./application/core/MY_Controller.php
 */
class BaseController extends CI_Controller
{
    
/**
     * Class properties - public, private, protected and static.
     * -----------------------------------------------------------------------
     */

    /**
     * Data variable for views.
     *
     * @var  array
     */
    
public $data = [];

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

        
log_message('debug'"BaseController Controller Class Initialized");
    }

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

  // End of BaseController Controller Class.

/**
 * Class Backend
 */
class Backend extends BaseController
{
    
/**
     * Class properties - public, private, protected and static.
     * -----------------------------------------------------------------------
     */

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

 
       log_message('debug'"Backend Controller Class Initialized");
    }

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

  // End of Backend Controller Class.

/**
 * Class Frontend
 */
class Frontend extends BaseController
{
    
/**
     * Class properties - public, private, protected and static.
     * -------------------------------------------------------------------
     */

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

        
log_message('debug'"Frontend Controller Class Initialized");
    }

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

  // End of Frontend Controller Class.

// End of MY_Controller Controller Class.

/**
 * -----------------------------------------------------------------------
 * Filename: MY_Controller.php
 * Location: ./application/core/MY_Controller.php
 * -----------------------------------------------------------------------
 */ 

Hope that helps.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Interesting.

So you don't necessarily need to have a class called MY_Controller inside the file MY_Controller.php?

I will give that a try.
Reply
#4

There are other ways to make this work. HERE is a page that discusses all the variations.

I am not a fan of putting more than one class in a file but it does work. I prefer using the "hooks" method as described on the linked page.

You can put your autoload into config.php and fix the conflict problem with simple changes.

PHP Code:
function my_autoloader($class)
{
 
  if (strpos($class'CI_') !== 0)
 
  {
 
      if (file_exists($file APPPATH 'core/' $class '.php'))
 
      {
 
          include $file;
 
      }
 
      elseif (file_exists($file APPPATH 'libraries/' $class '.php'))
 
      {
 
          include $file;
 
      }
 
  }
}

spl_autoload_register('my_autoloader'); 

By using spl_autoload_register() you allow multiple autoload functions to live in harmony - including the composer autoloader.
Reply
#5

@Dave friend

Thank you so much - that is an excellent guide to the different approaches. I went with the hooks method as it feels "proper" and it works perfectly alongside composer_autoload.

Many thanks for the super helpful answer!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB