Codeigniter error log messages - php

I have been using codeigniter for sometime now. But I am still new to it. And when I bump into some problem, its takes days to figure it out and solve it.
Unable to locate the model you have specified: page_m
I usullay face these sort of errors too. However since I am using multiple hierarchy of classes (Controllers) I am not being able to figure it out.
Is there any way I could make codeigniter to log the error messages with the line number??

You have to create model in Application/Models directory. and make sure you extend your CI_Model class.
eg. model name : my_model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class my_model extends CI_Model {
public function __construct() {
$this->load->database();
}
}

Are you sure that the file name is the same as the name of the class?
If your file is page_model.php the class must start with class Page_Model extends CI_Model
And in your controller remember $this->load->model('page_model');

Related

CodeIgniter will not load a specific controller

I am having a weird issue in which CodeIgniter (3.1) will not load a specific controller. I can load other controllers, but when I create a controller with the name sppb, using any case combination, and save the file as sppb.php it does not load.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Sppb extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
}
}
Above is the entire controller and it is saved to the server with the corresponding name of sppb.php.
I have also taken a different controller that I can load, copied it into a new file, changed the name to sppb, saved it to the server and it still will not load.
This is on a Linux server and I have checked the case in the naming of the file and the Controller.
Your Class file name needs to be 1st letter upper case... like
Sppb.php
The same applies to your class names
class Sppb extends CI_Controller {

Codeigniter - Extending my own controllers

I'd like to know if it's possible to extend my own controllers. I've been working on web-based applications for a while and I'm now starting to find each customer that wishes to use the application has different requirements of how it should work. My thoughts are that if I generate a base structure and then extend the controllers to override any of the functions that the customers require to work differently.
First of all, could you tell me if I'm on the correct track, and secondly, how do I go about extending my own controllers (if I can)? I've tried the usual:
Class Reports2 extends Reports { }
This doesn't work but I'm guessing it has something to do with the location of the file I'm trying to extend. My file structure is as follows:
Application
--->controllers
-------->control_panel
------------>reports.php
If I am not mistaken then you should be able to easily do this:
reports2.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH.'controllers/control_panel/reports.php');
Class Reports2 extends Reports {
public function __construct(){
parent::_construct();
}
public function index(){
}
}

Production Error:Unable to locate the model you have specified

I am developing on codeigniter from last two months. But when I pushed my code to Web hosting site today, I am getting this error. Few things I have checked 100 times:
Controller method says $this->load->model('myModel');
In the model folder I have myModel.php
It starts with
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');<br/>
class MyModel extends CI_Model {
public function __construct()
{
parent::__construct();
}
I have checked spelling mistakes or some typo but everything seems to be fine.
A name of model should be lower case: mymodel_model.php on /application/models/ directory.
The mymodel_model.php contains:
class Mymodel_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
And call it as:
$this->load->model('mymodel');
A _model string MUST be on:
filename of model mymodel_model.php
a class name with first uppercase name class Mymodel_model
Full example on Codeigniter manual: http://ellislab.com/codeigniter/user-guide/general/models.html
make the name of your model lower case all ex: "mymodel.php" and then in autoload.php just put this code $autoload['libraries'] = array('session','database'); Just get the parent construct int the model and make it simplier like this code.
<?php
class MyModel extends CI_Model
{
public function functionName()
{
}
}
Model filename should be all lowercase. #ddjikic's reply was correct and _model is not necessary.
I am using codeigniter 3.0.0 and I just faced same issue in my application when I hosted it to the production site. I salved it using the capitalizing the filename (first letter) as mentioned in error.
The Capitalize and lowercase filenames are dependent on different hosting servers.
I just made the filename's first letter capital and it works fine for me. I don't know what settings my hosting server have. but it worked for me.
You can also try, hope it can salve your error.
Example:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Enquiry_model extends CI_Model
{
}
As in above example my filename was enquiry_model.php when it gives me error.
But later I renamed it to Enquiry_model.php and it salved my error.

How can I get the __construct() in MY_Controller to run? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Codeigniter extending extended MY_Controller
I have been fiddling with this problem for a while now, and am at the point of frustration. I am extending the controller class with MY_Controller.
Here are the details:
__construct() does not seem to be called.
The file is located in application/core as it should be
The file is named MY_Controller.php
I have MY_ set up as the prefix for extensions (I have other extensions which work fine)
Here is my code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
echo 'TESTING!!!'; // This should print, followed by an ob_start() error
}
}
I should get a page back that says TESTING!!! followed by an ob_start() error, but instead the page renders normally.
I have tried splitting up the constructor class and just calling a private function within MY_Controller, but that doesn't work. I know the file is being called because if I purposefully create a php error within MY_Controller.php, the page will error out. It seems to me that the __construct() is just not running.
Can you show an example of one your controllers under application/controllers? Pls note that the controllers under application/controller should extend MY_Controller instead of CI_Controller - I think this is what you missed.
For example, under your controller application/controller/test.php. it should look like this:
class Test extends MY_Controller {
public function index()
{
echo 'test';
}
}

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