Welcome Guest, Not a member yet? Register   Sign In
Extanding the CI_Controller ot working
#1

[eluser]YahyaKACEM[/eluser]
hi, i'm using the Codeigniter.2.1.3 for a website, so i need to extend the CI_Controller so i can add a method to be executed with all controllers so i did what's in the user_guide:
creating a file named MY_Controller.php in the application/core folder the creating in it MY_Controller Class that extends the CI_Controller, the changing my regular controller to extend the MY_controller like this:
MY_controller.php:
Code:
class MY_Controller extends CI_Controller{
protected $page;
# Constructor
function __construct (){
  parent::__construct();
  #code shared with all controllers
}
public function get_page(){
            #code to get_the right page here
}
}
regular controller named Regular.php:
Code:
class Regular extends MY_Controller{
public function __construct(){
  parent::__construct();
}
public function index(){
  $this->get_page();
}
}
but the following error keep appearing:
Fatal error: Class 'MY_Controller' not found in /var/www/immo/CodeIgniter_2.1.3/application/controllers/regular.php on line 2
thanx in advance.
#2

[eluser]PhilTem[/eluser]
Are your permissions set correctly? That is can the webserver user access and read the file "MY_Controller.php"? And do you have the PHP opening tags in your file? Yet no PHP closing tags?
#3

[eluser]YahyaKACEM[/eluser]
yes the permission are set correctly 777 for the MY_Controller file and yes there is a php opening tag and no closing tag.
#4

[eluser]InsiteFX[/eluser]
If you are running on a live server make sure all of your filenames are all lower case, some servers are case sensitive.
#5

[eluser]YahyaKACEM[/eluser]
not a live server and the codeigniter wants the extended file name with the prefix MY_ and the rest of the class name capitalized the problem is that the MY_Controller is not autoloaded for some reason maybe i should do that manually.
#6

[eluser]InsiteFX[/eluser]
Add this to the bottom of your application/config/config.php file

Code:
/*
| -------------------------------------------------------------------------
| Native spl_autoload_register() - by Kenneth Vogt
| -------------------------------------------------------------------------
|
| Here is an updated version of Phil Sturgeon’s code:
|
| Thanks to Phil Sturgeon and Kenneth Vogt.
|
| NOTE:
| Requires PHP 5.3.+
| -------------------------------------------------------------------------
| MODIFIED by InsiteFX
| As of CI 3.0 Dev - The constant EXT has been removed modified
| to use '.php' now instead of EXT.
| should work for all version of CI and PHP 5.3
| -------------------------------------------------------------------------
| Place at the bottom of your ./application/config/config.php file.
| -------------------------------------------------------------------------
*/
spl_autoload_register(function($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;
  }
}
});
#7

[eluser]jmadsen[/eluser]
[quote author="InsiteFX" date="1353478395"]Add this to the bottom of your application/config/config.php file

Code:
/*
| -------------------------------------------------------------------------
| Native spl_autoload_register() - by Kenneth Vogt
| -------------------------------------------------------------------------
|
| Here is an updated version of Phil Sturgeon’s code:
|
| Thanks to Phil Sturgeon and Kenneth Vogt.
|
| NOTE:
| Requires PHP 5.3.+
| -------------------------------------------------------------------------
| MODIFIED by InsiteFX
| As of CI 3.0 Dev - The constant EXT has been removed modified
| to use '.php' now instead of EXT.
| should work for all version of CI and PHP 5.3
| -------------------------------------------------------------------------
| Place at the bottom of your ./application/config/config.php file.
| -------------------------------------------------------------------------
*/
spl_autoload_register(function($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;
  }
}
});
[/quote]

You don't need that code - it only allows you to extend other controllers from MY_Controller & then extend from those.

#8

[eluser]InsiteFX[/eluser]
@jmadsen - I know that I wanted to see if he was having an autoload issue. But I always use that code because I have Admin and Public Controllers.

@YahyaKACEM
Make sure you saved the MY_Controller to application/core/MY_Controller.php
#9

[eluser]YahyaKACEM[/eluser]
still not working now it gives me 2 new warnnings:

include_once(application/core/MY_Controller.php): failed to open stream: No such file or directory

include_once(): Failed opening 'application/core/MY_Controller.php' for inclusion (include_path='.:/var/www/ZendFramework/library')

no idea what that means and the application/core/MY_Controller.php exsists and it's readable.
#10

[eluser]InsiteFX[/eluser]
Here is my MY_Controller template:
Save to application/core/MY_Controller.php

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* ------------------------------------------------------------------------
* Created by phpDesigner 8.
* Date  : 11/21/2012
* Time  : 2:19:52 AM
* Author: Raymond L King Sr. - The Learn CodeIgniter Development Team.
* ------------------------------------------------------------------------
*
* Class MY_Controller
*
* ------------------------------------------------------------------------
* To change this template use File | Settings | File Templates.
* ------------------------------------------------------------------------
*/

class MY_Controller extends CI_Controller {

/**
  * --------------------------------------------------------------------
  * Class variables - public, private, protected and static.
  * --------------------------------------------------------------------
  */


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

/**
  *  __construct
  *
  * Class Constructor PHP 5+
  *
  * @access public
  * @return void
  */
  public function __construct()
  {
   parent::__construct();

}



} // End of Class.


/* ------------------------------------------------------------------------
* End of file MY_Controller.php
* Location: ./application/core/MY_Controller.php
* ------------------------------------------------------------------------
*/




Theme © iAndrew 2016 - Forum software by © MyBB