Code Igniter Inheritance error - php

Well i have defined a derived class from CI_Controller known as application that is present in the system/core/folder
<?php
Class Application extends CI_Controller {
function __construct() {
parent::__construct();
}
}
?>
and derived an class from application named as home which displays the home page
<?php
Class Home extends Application {
function __construct() {
parent::__construct();
}
public function index() {
$this->load->view("Home");
}
}
?>
But i still get an error saying that Class 'Application' is not found

Change core class name from 'Application' to 'MY_Controller'(class name must ends with _Controller) and save it as MY_Controller.php
Class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
}
}
Then extend the application controller class
Class Home extends MY_Controller {
You can use any words instead of MY_. For this change the following line in config.php
$config['subclass_prefix'] = 'MY_';
Make sure only Uppercase letters are allowed.

Related

The page you requested was not found codeigniter 3

I have setup the codeigniter, default controller is working(welcome.php) but when i add new controller its not working
Default controller :
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
}
public function testing()
{
$this->load->view('test');
}
}
http://localhost/appointment/index.php/welcome/ (working)
New controller
class democontroller extends CI_Controller {
public function index()
{
$this->load->view('test');
}
}
http://localhost/appointment/index.php/democontroller/ (not working)
Change the following code:
class democontroller extends CI_Controller {
}
to
class Democontroller extends CI_Controller {
}
and save this file with name Democontroller.php and try again.
Note: controller name first character must be capital as per naming convention.

Class 'BaseController' not found

I am using PHP MVC CI. BaseController and LoginController are both inside Controller Folder. When I run the Login Controller. It says..
Class 'BaseController' not found
Login Controller
<?php
if ( ! defined('BASEPATH')) die('No direct script access allowed');
class loginController extends BaseController {
function __construct() {
parent::__construct();
}
public function login() {
$this->load->View("template/header");
$this->load->View("login_view");
}
}
?>
Base Controller
<?php
class BaseController extends CI_Controller {
function __construct()
{
session_start();
$this->load->library('session');
parent::__construct();
}
}
?>
Any idea why this is happening ?
If you need a parent controller than should be in
Path - application/core/MY_Controller.php
http://www.codeigniter.com/user_guide/general/core_classes.html
<?php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
}
If you also autoload sessions then you will not need to use session_start.
http://www.codeigniter.com/user_guide/libraries/sessions.html
Filename: Welcome.php must be first letter uppercase in CI3
<?php
class Welcome extends MY_Controller {
}
CI 3 is case sensitive
Both user guides now here. CI2 & CI3 http://www.codeigniter.com/docs
You just need to change the file path , as follows:
application > controllers > Login.php
<?php
if ( ! defined('BASEPATH')) die('No direct script access allowed');
class loginController extends BaseController {
function __construct() {
parent::__construct();
}
public function login() {
$this->load->View("template/header");
$this->load->View("login_view");
}
}
?>
And,
application > core > Base_controller.php
<?php
class BaseController extends CI_Controller {
function __construct()
{
session_start();
$this->load->library('session');
parent::__construct();
}
}
?>
Codeigniter wont load it from the same folder.
You either put both classes in the same file, not desirable or put the base controller in application/core.
You will need to also set your prefix for you extended controller, for example BASE_Controller.
application/config/config.php
$config['subclass_prefix'] = 'BASE_';
Codeigniter has good docs, and what you are after can be found here

Access subclass in codeigniter 3.0.1

I will extend the controller based of login typical, so I create on application directory like this:
core
-MY_Controller
-public controller
MY_Controller
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
}
public controller
<?php
class Public_Controller extends MY_Controller
{
//Some Logic here
public $layout = 'layout';
}
Now, time to use those things.
I write on application/route $route['default_controller'] = 'Home';
So, the controll that named Home would be like this :
<?php
class Home extends Public_Controller {
public function index() {
$this->load->view('public/home');
}
}
But unfortunately, it gives me error like this :
Fatal error: Class 'Public_Controller' not found in C:\wamp\www\egi\application\controllers\Home.php on line 5
Why I can not to extends the sub class ?
NOTE
But if I extends from MY_Controller, its success
<?php
class Home extends MY_Controller {
public function index() {
$this->load->view('public/home');
}
}
Any help it so appreciated
You can do this in one of the following ways.
Have a file named Public_controller in "application/core" & include that file in your application/core/MY_Controller.php file.
Define Public_Controller inside MY_Controller file which is autoloaded by default which means you'd end up with something like this.
File: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
public function __construct () {
parent::__construct();
}
}
class Public_Controller extends CI_Controller {
public function __construct () {
parent::__construct();
}
}

Extends Model in CodeIgniter

Well, I'm newbie in CodeIgniter Framework and i'm trying building a generic Model Class. See:
class Basic_Model extends CI_MODEL {
function __construct() {
// Call the Model constructor
parent::__construct();
}
}
I want to extends all Models based on Basic_Model, like this:
class Pagina_Model extends Basic_Model {
function __construct() {
// Call the Model constructor
parent::__construct();
}
}
The problem is when i try to call "pagina_model" from a Controller a got the following error:
Fatal error: Class 'Basic_Model' not found in /var/www/myproject/application/models/pagina_model.php on line 12
If i use "basic_model" in controller everything works fine.
EDIT 1:
I created a file named MY_Basic_Model.php in "/application/core" and changed the class name to "MY_Basic_Model". But i got the error:
Fatal error: Class 'MY_Basic_Model' not found in /var/www/myproject/application/models/pagina_model.php on line 12
For this you have to create Core System Classes (this is also known as Method Overriding).
Create a file MY_Model.php in application/core/ directory which will extend the base CI_Model class:
<?php
class MY_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
}
Now you can extend this in your models (../applicaton/models/):
<?php
class Pagina_Model extends MY_Model {
function __construct()
{
parent::__construct();
}
}
Few things to note here:
1) The class declaration must extend the parent class.
2) Your new class name and filename must be prefixed with MY_ (this item is configurable).
How to configure:
To set your own sub-class prefix, open your application/config/config.php file and look for this item:
$config['subclass_prefix'] = 'MY_';
Documentation:
https://ellislab.com/codeigniter/user-guide/general/core_classes.html
You can do it this way.
lets assume you have basic_model.php inside your model folder .
Now add the code for class Basic_Model that you have written
class Basic_Model extends CI_MODEL {
function __construct() {
// Call the Model constructor
parent::__construct();
}
}
Now make a pagina_model.php inside your model folder and add the code that you written. Just include the first line like bellow
<?php
require APPPATH.'/models/basic_model.php';//just add this line and keep rest
class Pagina_Model extends Basic_Model {
function __construct()
{
parent::__construct();
}
}
hope this will solve your problem
You can do this ... MY_ model is really good but should you wish a sub model to extend a different sub model you can always do:
require(APPPATH.'models/Other_model.php');
class New_model extends Other_Model {
In my instance Other_Model actually extends MY_model.

Fatal error: Class 'Model' not found in CodeIgniter

My CI version is CI2.3. I'm running this php code in my local host. I followed all the steps given there but I'm getting this error don't know why? and I changed Controller to CI_Controller. Hello world Program worked finely. This link code is not working. please need help!
you should extend model like this in codeIgniter
class Modelname extends CI_Model
{
function __construct()
{
parent::__construct();
}
}
Well actually the study guide is of old version of CI, where you used to extend your models from Model class as show in the guide. But now it has been changed. Now you have to extend it from CI_Model, same goes for Controller.
For controllers
class Employee_controller extends CI_Controller
{
//load the constructor
function __construct()
{
//inherit the parent constructor
parent::__construct();
}
}
and for models:
class Employee_model extends CI_Model
{
//load the constructor
function __construct()
{
//inherit the parent constructor
parent::__construct();
}
}
Use like this
<?php
class Employee_model extends CI_Model
{
//load the constructor
function __construct()
{
//inherit the parent constructor
parent::__construct();
}
}
You must create a model in the model folder like my_model.php
And create the class like
class My_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
}
Remember the class and the file should be same.
Docs http://ellislab.com/codeigniter/user-guide/general/models.html

Categories