EllisLab text mark
Advanced Search
     
Extanding the CI_Controller ot working
Posted: 20 November 2012 06:33 PM   [ Ignore ]
Joined: 2012-11-20
15 posts

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:

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:

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.

 
Posted: 20 November 2012 07:40 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2011-02-23
882 posts

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?

 Signature 

ignited Community Framework (WiP)  |  Read the User’s Guide. It won’t bite.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

CI example .htaccess

 
Posted: 21 November 2012 12:43 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2012-11-20
15 posts

yes the permission are set correctly 777 for the MY_Controller file and yes there is a php opening tag and no closing tag.

 
Posted: 21 November 2012 12:58 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6267 posts

If you are running on a live server make sure all of your filenames are all lower case, some servers are case sensitive.

 Signature 

Ceritfied State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 21 November 2012 01:26 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2012-11-20
15 posts

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.

 
Posted: 21 November 2012 02:13 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6267 posts

Add this to the bottom of your application/config/config.php file

/*
| -------------------------------------------------------------------------
| 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;
  
}
 }
}
); 

 

 Signature 

Ceritfied State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 21 November 2012 02:27 AM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2009-04-15
453 posts
InsiteFX - 21 November 2012 02:13 AM

Add this to the bottom of your application/config/config.php file

/*
| -------------------------------------------------------------------------
| 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;
  
}
 }
}
); 

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

 

 Signature 

Code By Jeff

Mahana Messaging Library

Problem with your query? Did you run

$this->db->last_query(); 

before you came to the forums for help?

 
Posted: 21 November 2012 02:53 AM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6267 posts

@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

 Signature 

Ceritfied State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 21 November 2012 02:56 AM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Joined: 2012-11-20
15 posts

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.

 
Posted: 21 November 2012 03:22 AM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6267 posts

Here is my MY_Controller template:
Save to application/core/MY_Controller.php

<?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
 * ------------------------------------------------------------------------
 */ 

 

 Signature 

Ceritfied State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 21 November 2012 09:02 AM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Joined: 2012-11-20
15 posts

still nothing, the same errors.

 
Posted: 21 November 2012 11:11 AM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Joined: 2012-11-20
15 posts

this is so embarrassing it was just the wrong file name instead of MY_Controller.php i save the file with the name MY_Contorller.php and @jmadsen & @InsiteFX no need for the autoload function in the config file it’s already autoloaded. sorry for that and thanx for your help.

 
Posted: 21 November 2012 11:30 AM   [ Ignore ]   [ # 12 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6267 posts

You only need the autoload in the config if you extend more Controllers from the MY_Controller.

Where I have two other ones Admin_Controller and Public_Controller

application/core/
-- 
MY_Controller extends CI_Controller
-- Admin_Controller extends MY_Controller
-- Public_Controller extends MY_Controller 

Then you need the config autoload or it will not see the other Controllers.

 

 Signature 

Ceritfied State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 21 November 2012 12:06 PM   [ Ignore ]   [ # 13 ]   [ Rating: 0 ]
Joined: 2012-11-20
15 posts

thank you, i think i’m about to add an admin an public controllers you probably saved me some time with this reply, thanx.

 
Posted: 21 November 2012 02:01 PM   [ Ignore ]   [ # 14 ]   [ Rating: 0 ]
Joined: 2011-02-23
882 posts
InsiteFX - 21 November 2012 11:30 AM

You only need the autoload in the config if you extend more Controllers from the MY_Controller.

Where I have two other ones Admin_Controller and Public_Controller

application/core/
-- 
MY_Controller extends CI_Controller
-- Admin_Controller extends MY_Controller
-- Public_Controller extends MY_Controller 

Then you need the config autoload or it will not see the other Controllers.

Do you really need to do this? I have some hierarchical controllers and loading them works fine

application/core/
  class 
MY_Controller extends MX_Controller {}
  
class Public_Controller extends MY_Controller {}
  
class Authenticated_Controller extends Public_Controller {}
  
class Admin_Controller extends Authenticated_Controller {}
  
class HMVC_Controller extends MX_Controller {}
  
class AJAX_Controller extends MY_Controller {} 

Or does it work for me because I’m using HMVC?

 Signature 

ignited Community Framework (WiP)  |  Read the User’s Guide. It won’t bite.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

CI example .htaccess

 
Posted: 21 November 2012 02:36 PM   [ Ignore ]   [ # 15 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3801 posts

HVMC has it’s own built-in autoload, so yes.

 Signature