2

I want to extend basic controller by my own. This is code of file MY_Secure.php which i put in application/libraries

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

Class MY_Secure extends CI_Controller {
  public function __construct() {
    parent::__construct();
    if(!$this->session->userdata('status') == 'admin') {
        $data['message'] = '<p class="error">You shold login in admin area</p>';
        $this->load->view('admin/login', $data);
    }
  }
}
?>

And here is my controller in application/controllers/category.php

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

class Category extends MY_Secure {

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

 public function index()
{
 $data['login'] = $this->session->userdata('login');
 $data['status'] = $this->session->userdata('status');

 $this->load->model('categories_model');
 $data['main_categories'] = $this->categories_model->get_main_categories();

 $this->load->view('admin/headers/main', $data);
 $this->load->view('admin/category');
 $this->load->view('admin/footer/main');
}
?>

But i have an error Fatal error: Class 'MY_Secure' not found in Z:\home\ci.local\www\application\controllers\admin\category.php on line 3 How can i avoid this error? What i do wrong?

5 Answers 5

6

http://codeigniter.com/forums/viewthread/182186/#862394

...so put MY_Secure.php MY_Controller.php in application/core

Sign up to request clarification or add additional context in comments.

6 Comments

That`s meen that i could have only one controller, which can extends basic controllers?
at this stage I am satisfied by one controller, thanks. But what shoul`d i do, when i have a need to another one?
No, you can have as many controllers as you want. But extending the core controller is not the same thing as making new controllers. stackoverflow.com/questions/5613767/… codeigniter.com/user_guide/general/controllers.html
i extnad contrler like this I extand URL of codeingniter with basic url structe: <? php class save_user_contrller extends MY_COntroller { function save_users() { $this -> usersModel -> save(MD5 ($this->input->post('login'))); }
Mr westley: extnad is not yet a valid codnighter variance. Not until the next release.
|
1

There is no required condition to save or name it is as MY_Controller. You can name it eg: Mysite_Controller if you'd like.

Please put your class in the application/core folder and then extend this class from your controller class.

But in order for that to work properly, you need to change config file value:

$config['subclass_prefix'] = 'Mysite_'; // default  it is 'MY_'

Comments

0

And you can not name your extends controller 'MY_Secure', you must name it 'MY_Controller'. Then extend it

class Category extends MY_Controller

Comments

0

I think the problem is the location of MY_Secure.php. The MY_ prefix works if you have not changed it in the config.

MY_Secure class must be saved in Applications Core folder for it to work in CodeIgniter 2.O not in Libraries folder.

Comments

0

Phil Sturgeon has a really nice blog post on this: http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-base-Classes-Keeping-it-DRY

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.