Luciano Victor
Luciano Victor

Reputation: 21

Codeigniter custom controller not loading anything

I have a MY_Controller that extends the CI_Controller, the MY_Controller will hold all commom features of the app. A Frontend_Controller that extends the MY_Controller, this controller will hold all public related stuff. A Admin_Controller that will hold all administrative stuff, this controller also extends the MY_Controller, and some specfic controllers that extends Frontend_Controller or Admin_Controller.

I'm using a hook to autoload the controllers, here are my files:

application/core/MY_Controller.php

class MY_Controller extends CI_Controller
{
    protected $data = array(); 

    function __construct()
    {
        parent::__construct();
        $this->data['errors'] = array();
        $this->data['site_name'] = $this->config->item('site_name');
    }
}

application/core/Frontend_Controller.php

class Frontend_Controller extends MY_Controller
{

    function __construct()
    {
        parent::__construct();
        var_dump ('Hello');
    }
}

application/controllers/Welcome.php

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

class Welcome extends Frontend_Controller {

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

    public function index()
    {
        var_dump($this->data);
        $this->load->view('welcome_message');

    }
}

My hook configuration

application/hooks/App_controllers.php

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

<?php
function load_app_controllers()
{
  spl_autoload_register('my_own_controllers');
}

function my_own_controllers($class)
{
  if (strpos($class, 'CI_') !== 0)
  {
    if (is_readable(APPPATH . 'core/' . $class . '.php'))
    {
      require_once(APPPATH . 'core/' . $class . '.php');
    }
  }
}

application/config/hooks.php

/*
| -------------------------------------------------------------------------
| Hooks
| -------------------------------------------------------------------------
| This file lets you define "hooks" to extend CI without hacking the core
| files.  Please see the user guide for info:
|
|   https://codeigniter.com/user_guide/general/hooks.html
|
*/

$hook['pre_system'][] = array(
  'class' => '',
  'function' => 'load_app_controllers',
  'filename' => 'App_controllers.php',
  'filepath' => 'hooks'
);

In the config.php the hook is enabled $config['enable_hooks'] = TRUE;

My problem is that the Welcome controller is loading a blank page, when I created the MY_Controller and tested on it, worked fine, but when I created the Frontend_Controller and tried only loaded a blank page.

Upvotes: 1

Views: 609

Answers (1)

Luciano Victor
Luciano Victor

Reputation: 21

The error was in the App_controller.php file, it had two <?php tags.

Upvotes: 1

Related Questions