Welcome Guest, Not a member yet? Register   Sign In
Custom Controller
#1

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 ?
Reply
#2

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?
Reply
#3

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';

"I reject your reality and substitute my own" - Adam Savage, M5 Inc.
Reply
#4

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';

Reply
#5

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';

Reply
#6

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

(This post was last modified: 11-27-2014, 01:19 PM by sv3tli0.)

This hack is ok but why do you need 2 CORE controllers ?
Best VPS Hosting : Digital Ocean
Reply
#8

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.
Reply
#9

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

It will work fine.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB