CodeIgniter Forums
Custom Controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Custom Controller (/showthread.php?tid=386)



Custom Controller - kylevorster - 11-27-2014

Hey there,

I'm having no luck with a custom controller I'm trying to use, I've got two, the first one works 100% but the second one is giving me problems. Here's my setup.

My Working Controller

# /config/config.php
PHP Code:
$config['subclass_prefix'] = 'SH_'

# /core/SH_Controller
PHP Code:
class SH_Controller extends CI_Controller  {

 
 public function __construct()
 
 {
 
   parent::__construct();
 
 }


# Controllers/home.php
PHP Code:
class Home extends SH_Controller {

 
 function __construct()
 
 {
 
   parent::__construct();
 
 }


And the one that's not working.

# core/SH_Shopping.php
PHP Code:
class SH_Shopping extends CI_Controller  {

 
 public function __construct()
 
 {
 
   parent::__construct();
 
 }


# controllers/store.php
PHP Code:
class Store extends SH_Shopping {

 
 function __construct()
 
 {
 
   parent::__construct();
 
 }


I've read everything online about adding __autoload in the config etc but nothing seems to work ... Any idea's ?


RE: Custom Controller - mlantz - 11-27-2014

Take a look at the core system files in the CodeIgniter.php file you can see how the subclass prefix is used when loading controllers.

Line 237 - 240

if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
{
require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
}

When you name it other than _Controller the system does not seem to register that, in order to have a Controller that does not end with _Controller you're out of luck. Is there a reason you need to run multiple controller types? Is there a library solution that would work just as well?


RE: Custom Controller - Hobbes - 11-27-2014

I had an issue a while back with just about every method to get CI to load my admin_controller from the core folder. so what i did was at the end of the MY_Controller file i added a require_once statement. 

so if your set on doing it this way two things,

first make your SH_Shopping controller extend SH_Controller. That way any functionality in SH_Controller will be available to your SH_Shopping controller.

then the next step would be to make your SH_Controller class file look like this:

PHP Code:
class SH_Controller extends CI_Controller  {
  public function __construct()
  {
    parent::__construct();
  }


if ( ! 
class_exists('SH_Shopping'))
{
 
 require_once APPPATH.'core/SH_Shopping.php';




RE: Custom Controller - kylevorster - 11-27-2014

Thank you for that code Hobbes, I think that's exactly what I will do.

Correct me if I'm wrong but should the include not read.

PHP Code:
if ( class_exists('SH_Shopping'))
{
  require_once 
APPPATH.'core/SH_Shopping.php';




RE: Custom Controller - kylevorster - 11-27-2014

That must have been the most dumpest post I've ever made in my entire life, please ignore

(11-27-2014, 09:19 AM)kylevorster Wrote: Thank you for that code Hobbes, I think that's exactly what I will do.

Correct me if I'm wrong but should the include not read.


PHP Code:
if ( class_exists('SH_Shopping'))
{
 
 require_once APPPATH.'core/SH_Shopping.php';




RE: Custom Controller - Hobbes - 11-27-2014

lol no problem. Smile we are all prone to the proverbial blonde moment Smile


RE: Custom Controller - sv3tli0 - 11-27-2014

This hack is ok but why do you need 2 CORE controllers ?


RE: Custom Controller - includebeer - 11-27-2014

The "core" folder is to replace or extend an existing CI core class. You exdended the CI_Controller class with SH_Controller, that's ok. But Shopping is not a core class. There's no CI_Shopping class in CodeIgniter. You need to put this file in the "controllers" folder.


RE: Custom Controller - Rumolt - 11-27-2014

Add the code (in core/SH_Shopping.php) to the file /core/SH_Controller.php

It will work fine.