I am new to codeigniter and I was trying my first app from the reference of this link
But I got the following error:
Fatal error: Class 'Controller' not found in /var/www/CodeIgniter/application/controllers/helloworld.php on line 2
Thi should be done like this
Class Mycontroller Extends CI_Controller
{
function __construct(){
parent::__construct();
}
//other functions
}
Class Mymodel Extends CI_Model
{
function __construct(){
parent::__construct();
}
//other functions
}
Related
i am trying to make a core class in my codeigniter but its giving error that Unable to locate the specified class: Loader.php.
My Core class is MY_base.php and code is.
class MY_base extends CI_Controller{
public function load_header(){
$this->load->model('mod_practice');
$headData=$this->model->get_header();
$this->load->view('header',$headData);
}
}
My model Mod_practice.php code is
class Mod_practice extends CI_Model{
public function get_header(){
$query = $this->db->get('header');
$result = $query->result_array();
return $result;
}
}
My home.php ( main controller) code is
class Home extends MY_loader{
function index(){
parent::MY_base();
}
}
but when i try to run Home controller its giving me the following error
Unable to locate the specified class: Loader.php.
Where can be the error ? Thanks in advance.
You are doing something wrong. You need to construct CI_controller first.
class MY_base extends CI_Controller{
public function __construct()
{
parent::__construct();
// Your own constructor code
}
public function load_header(){
$this->load->model('mod_practice');
$headData=$this->model->get_header();
$this->load->view('header',$headData);
}
}
And now you can do this:
class Home extends MY_base{
public function __construct()
{
parent::__construct();
// Here you have access to load_header() function
}
}
Ou - and also your create MY_base and then refer to MY_loader.
may bee you can use HMVC Codeigniter https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
I have the following simplistic code:
// FILE: controllers/Top.php
class Top extends MY_Public_Controller {
function __construct() {
}
public function Top() {
echo 'Hello';
}
}
// FILE: application/core/MY_Public_Controller.php
class MY_Public_Controller extends MY_Controller {
function __construct() {
parent::__construct();
}
}
// FILE: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
}
}
And I get the following the following error:
Fatal error: Class 'MY_Public_Controller' not found in
/var/www/example.com/public_html/application/controllers/Top.php on line 5
A PHP Error was encountered
Severity: Error
Message: Class 'MY_Public_Controller' not found
Filename: controllers/Top.php
Line Number: 5
Backtrace:
Any help would be much appreciated!
Instead of you create a new file (MY_Public_Controller.php) to create the class My_Public_Controller.
Insert this class inside the My_Controller.php file.
In that way the My_Controller.php file will be like:
class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
}
}
class MY_Public_Controller extends MY_Controller {
function __construct() {
parent::__construct();
}
}
After I see another answer
Or you can make something like #Hikmat Sijapati said, but instead of you put the require_once, inside the My_Controller.php. Try to put it in the My_Public_Controller.php using 'My_Controller.php' as parameter. Something like that:
My_Public_Controller.php:
include_once('My_Controller.php');
class MY_Public_Controller extends MY_Controller {
function __construct() {
parent::__construct();
}
}
I have not tried it that way, but I think it will work.
Try like this...
You can create any number of controller but create controller's must be included in the controller that extends CI_Controller.As Below:
Controller's Name and function Name Keep different (Good Way)
MY_Controller:application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
}
include_once('MY_Public_Controller.php');// include here
}
MY_Public_Controller: application/core/MY_Public_Controller.php
class MY_Public_Controller extends MY_Controller {
function __construct() {
parent::__construct();
}
}
And Top: application/Top.php
class Top extends MY_Public_Controller {
function __construct() {
}
public function index() { //function name must be different than controller's name
echo 'Hello';
}
}
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.
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('login');
$this->load->model('profile_model');
$this->load->model('common_model');
}
function index()
{
$this->user_information();
}
function user_information()
{
if($this->input->post('submit'))
{
}
else
{
$data['settings']=$this->common_model->get_user_settings();
$this->load->view('profile/user-settings',$data);
}
}
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Settings::$common_model
Filename: controllers/settings.php
Line Number: 24
Fatal error: Call to a member function get_user_settings() on a non-object in D:\xampp\htdocs\dandalo\application\controllers\settings.php on line 24
This is my code. I am getting this error. I dont know how come this kind of error when I load the model correctly.
Model Code:
<?php
class common_model extends CI_Model
{
public $datet;
public function __construct()
{
parent::__construct();
$this->load->database();
//$this->load->model('buy_model');
$this->datet=date('Y-m-d H:i:s');
}
function get_user_settings()
{
$this->db->select('user_name,u_first_name,u_last_name,u_email,u_profile_image,fb_id');
$this->db->where('user_id',user_id());
return $this->db->get('en_user')->result();
}}?>
This is my model code. I also extended the CI_model. still I am getting error.
locate your model in application/models
profile_model.php
common_model.php
your model class looks like
class ProfileModel extends CI_Model
class CommonModel extends CI_Model
load in controller
$this->load->model('profile');
$this->load->model('common');
then call model function
$data['settings']=$this->common->get_user_settings();
For more :- http://ellislab.com/codeigniter/user-guide/general/models.html
Your model class name should be Start with first latter uppercase, It should be
class Common_model extends CI_Model
OR
class Common_Model extends CI_Model
Instead of
class common_model extends CI_Model
Where Model_name is the name of your class. Class names must have the first letter capitalized with the rest of the name lowercase. Make sure your class extends the base Model class.
take a look on http://ellislab.com/codeigniter/user-guide/general/models.html#anatomy
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