In Codeigniter extends one controller into another controller - php

I am using codeigniter(3.1.5) and Ihave two controllers in my application/controllers/ folder. with name controller A and Controller B. I want to extends Controller A in Controller B so that I can use methods of Controller A. But it generates class not found error.
Here is my sample code:
A_Controller.php:
defined('BASEPATH') OR exit('No direct script access allowed');
class A_Controller extends CI_Controller {
public function index()
{
}
public function display(){
echo 'base controller function called.';
}
}
B_Controller.php:
defined('BASEPATH') OR exit('No direct script access allowed');
class B_Controller extends A_Controller {
}
I want to execute display() method of controller A in controller B.
If i put controller A in application/core/ folder and in application/config/config.php file make
$config['subclass_prefix'] = 'A_';
then I can able to access methods of controller A.
Please suggest.
Thanks in advance.

Extending a controller in another controller is not really good. Building with MVC and especially with CI, you have other options to achieve this.
Use a class MY_Controller inside application/core that extends the CI_Controller. Later, all (or some) of your controllers should extend the MY_Controller. In MY_Controller you can have many functions and you can call which function you want in your controller.
Use a library. Write your own library in application/libraries and load it in your controller wherever you want. A library is a class with functionality for your project.
Use a helper. Write your own helper in application/helpers and load it in your controller. A helper should have a general purpose for your application.
In that way, your code will be more flexible and reusable for the future. Messing with 2 Controllers seems bad to me. Remember that with the default Routing system of CI you can be confused.

Try to use following code.
defined('BASEPATH') OR exit('No direct script access allowed');
class B_Controller extends A_Controller {
public function test()
{
$this->display();
}
}

Use construct in controller
defined('BASEPATH') OR exit('No direct script access allowed');
class B_Controller extends A_Controller {
public function __construct() {
parent::__construct();
}
public function test()
{
$this->display();
}
}

I found the solution using including parent controller on child controller like this -
require_once(APPPATH."modules/frontend/controllers/Frontend.php");
then my function like this -
class Home extends Frontend {
function __construct() {
parent::__construct();
}
function index() {
echo $this->test(); //from Frontend controller
}
}
I hope this will help.

add this script in B_Controller :
include_once (dirname(__FILE__) . "/A_Controller.php");
for example B_Controller.php :
defined('BASEPATH') OR exit('No direct script access allowed');
include_once (dirname(__FILE__) . "/A_Controller.php");
class B_Controller extends A_Controller {
}

Related

CodeIgniter - Hyperlink creation

I have tried to create a hyperlink to another page using anchors however it doesn't seem to work.
My function is called calendar and its stored in calcontrol.php within a controllers directory.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Calendar extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
$this->load->view('calendar');
}
}
?>
I have tried other methods but they do not seem to work.
edit : this is my a href
Link
Filename and classname must be the same, otherwise CI will not be able to load it. And if using CI 3, make sure your class name and filename are both Ucfirst. If you have everything set up like this, then you should be able to view the page through http://yourdomain.com/calendar/calendar
Try this code. You forgot to load url helper. File Calendar.php - nogice capital C if CI3:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Calendar extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
$this->load->view('calendar');
}
}

Add public variable to all CI_Controller's and anything that extends them?

Hi I'd like to add a public variable ($this->data[]) to all instances of CI_Controllers, that way I can store some base rules for outputting a page (css/js, etc) then have each controller append to this array to add its own requirements (more css/js). I have a core library with custom view functions that take those arrays and inject them into the head tag of the page template.
The options I've thought of;
Edit CI_Controller and add it there... guessing that's a bad idea.
Create a shell controller that extends CI_Controller, add the var to that, then have every other controller extend the shell controller.
Any other clever ways?
I've only been using CI for about a month and I've tried reading through the docs but I can't find any built in ways to do something like this? Has anyone encountered this before and if so how did you solve it?
Thanks!
[edit] Using PHP 5.3.x [/edit]
http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html
You can extend CI_Controller to have the functionality you want.
application/core/MY_Controller.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function my_function()
{
return "Cool return from my_function";
}
}
controllers/welcome.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends MY_Controller {
public function index()
{
echo $this->my_function();//echo's "Cool return from my_function"
}
}
You simply define the functionality you want in MY_Controller. Then in your controllers, use extends MY_Controller instead of extends CI_Controller and you can call the functions anywhere inside those controllers.
I think what you want can be easily achieved using traits. Check here: PHP: Traits
More specifically -> Example #11 Defining Properties.
The only limitation is it's PHP 5.4+
You can create your own a base controller file to inherit basic page load methods from in \application\core\MY_Controller.php as such:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
}
protected function loadPage($path, $data = array())
{
$this->load->view('common/head'); // Assuming you will use a folder for page parts
$this->load->view($path, $data);
$this->load->view('common/foot');
}
}
Afterwards in your page controller you can call upon the same methods much more easily:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class User extends MY_Controller {
public function index()
{
$data = array(); // get data from model
$this->loadPage('user/view', $data); // Assuming you will use folders for sets of views
}
public function edit()
{
$data = array(); // get data from model
$this->loadPage('user/edit', $data);
}
}

Call to a member function userdata() on a non-object

Hi everybody I have a code that give me this error
Fatal error: Class 'MY_Controller' not found in C:\wamp\www\project\application\controllers\admin\home.php on line 3
I have no idea why it's showing this error…
The code of C:\wamp\www\project\application\controllers\admin\home.php is
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends MY_Controller {
function index()
{
redirect('admin/login');
}
function logout()
{
$this->session->unset_userdata('logged_in');
//session_destroy();
redirect('admin/login');
}
}
?>
The code of C:\wamp\www\project\application\libraries\MY_Controller.php is
<?php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
if (!$this->session->userdata('logged_in')) {
redirect('admin/login');
}
}
}
And also if I place
class Home extends CI_Controller
instead of
class Home extends MY_Controller
in the
C:\wamp\www\project\application\controller\admin\home.php
file and try to load the
C:\wamp\www\project\application\libraries\MY_Controller.php
in the constructor of
C:\wamp\www\project\application\controllers\admin\home.php
it shows
Call to a member function userdata() on a non-object
Why so?
You need to put class files to core instead of library folder when you extending System classes. Put MY_Controller.php in core folder.
Refer to the documentation: http://codeigniter.com/user_guide/general/core_classes.html
Core controllers need to be stored in application/core/
So when you extend an object, it will look for it there. Library folder is used for storage of 'external' libraries, which you must explicitly include in your controller:
Ex:
$this->load->library('class name');
Info on libraries here: http://codeigniter.com/user_guide/general/libraries.html

Codeigniter libraries - Error loading custom classes

I am unable to load my custom class which doesnot extend from any core class.
I have placed my custom class in a subfolder inside application/libraries.
So here is my folder structure
application
|_ libraries
|_ cgh
|_ cgh_asset.php
|_ cgh_article.php
|_ cgh_asettype.php
|_ controllers
|_ welcome.php
Class Cgh_article is a subclass of Cgh_asset
Cgh_asset.php :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
abstract class Cgh_asset
{
public $id;
public $type;
public $title;
public $content;
public $user;
abstract public function generate_url();
function __construct()
{
$this->generate_url();
}
}
?>
Cgh_article.php :
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cgh_article extends Cgh_asset
{
function __construct()
{
parent::__construct();
$this->type=Cgh_assettype::article;
}
function generate_url()
{
$this->url="Article_URL";
}
}
?>
Cgh_assettype.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cgh_assettype
{
const type1="type1";
const type2="type2";
const article="article";
}
?>
Controller welcome.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->library('cgh/Cgh_assettype','cgh/Cgh_asset','cgh/Cgh_article');
$this->load->view('welcome_message');
}
}
The error I get is :
Unable to load the requested class: Cgh_assettype
I must have tried all possible upper and lower case combinations of classnames, filenames, but the error is always the same.
After going through some answers, I think probably I should add one basic question here - Is it at all possible to have my own custom object types within codeigniter ... types that should be quite obvious from my question ?
This seems to work for me, so here is what I will be doing ... at least till something breaks :
In the constructor of my controller, I use require_once for my classes ... and good thing is I can combine all my classes into a single file -- my classes initially were in a single file anyways --This is my controller after the changes, and this works :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public $cgh_assettype;
public $cgh_asset;
public $cgh_article;
function __construct()
{
parent::__construct();
//$this->load->library(array('cgh/cgh_assettype','cgh/cgh_asset','cgh/cgh_article'));
echo "Including CGH<br />";
echo "<p>Apppath is ". APPPATH. "</p>";
require_once(APPPATH.'libraries/cgh/Cgh_assettype.php');
require_once(APPPATH.'libraries/cgh/Cgh_asset.php');
require_once(APPPATH.'libraries/cgh/Cgh_article.php');
}
public function index()
{
$iCgh_article=new Cgh_article();
echo "<p>$iCgh_article->url</p>";
$this->load->view('welcome_message');
}
}
You need to call $this->load->library for each library.
$this->load->library('cgh/Cgh_assettype');
$this->load->library('cgh/Cgh_asset');
$this->load->library('cgh/Cgh_article');
$this->load->library takes 3 parameters.
File to load
(optional) $config array
(optional) String to rename library to ($this->Renamed_library)
If you want to load multiple libraries on one line, use an array as the 1st parameter.
$this->load->library(array('cgh/Cgh_assettype','cgh/Cgh_asset','cgh/Cgh_article'));
Are you library file names capitalized? (Your submitted folder structure says they are not).
I don't know about having libraries within sub-directories, but I know the file names need to be capitalized.
http://codeigniter.com/user_guide/general/creating_libraries.html
Naming Conventions
File names must be capitalized. For example: Myclass.php
Class declarations must be capitalized. For example: class Myclass
Class names and file names must match.
You should load library or whatever only once. If you're loading for second time, you get that error.

codeigniter -> having trouble loading multiple libraries/classes

Ok, so in my base controller (page.php) I have the following code which works fine:
$this->load->library('Siteclass');
$mysite = new site_model();
The siteclass library references a model named site_model and instantiates based on data received from that model. All is good.
Now I want to load another library so that I can instantiate another object as well. So I add this to page.php:
$this->load->library('Memberclass');
$mysite = new member_model();
But now I get the following error:
Message: Undefined property: Memberclass::$site_model
Filename: libraries/Loader.php
Line Number: 1035
From what I can tell, it seems that the loader class, when being applied to the Memberclass, is somehow still referencing the site_model instead of the member_model. I've checked my code and I am definitely calling the correct files.
Here's what Siteclass.php looks like:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Siteclass extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Site_model');
$data = $this->Site_model->load_site_data();
// etc etc
and here's what Memberclass.php looks like:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Memberclass extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Member_model');
$data = $this->Member_model->load_member_data();
// etc etc
Thanks in advance for any help!
Gary
I think you're confused about how MVC works in CodeIgniter. Why are you using the loader class to create a controller? Why are you creating a stand-alone instance of your model outside of your controller class?
In CodeIgniter, your URLs represent paths to your controllers' methods. That means that your "base controller" should automatically be instantiated if you go to:
www.example.com/memberclass
Or perhaps more to the point, if you have a link like this:
www.example.com/page
You should have a file in your /application/controllers directory called page.php which looks like this:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Page extends Controller {
function __construct() {
parent::Controller();
// etc etc
Furthermore, unless you're loading data from your model to be used every single time you call this controller, you'll want to put your model calls inside a non-constructor method of this class. Something like:
class Page extends Controller {
function __construct() {
parent::Controller();
}
function index() {
$this->load->model('Member_model');
$data = $this->Member_model->load_member_data();
$this->load->view('myview', array('data'=>$data));
}
}
So again...not entirely sure what context you're doing this all in, but it seems like you're not standing firmly within the framework. There's basically no reason you should be using the loader class to load controllers, and furthermore there's no reason you should be creating stand-alone instances of model classes using PHP's new keyword.

Categories