i started a website using codeigniter 3 -dev about couple of month ago , today i've upgrade it to latest version on github .... it works fine on localhost but when i upload it on server it couldn't find the models !!
http://epatil.com/ci_test
my only controller :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->model('test');
$this->test->echo_print();
$this->load->view('welcome_message');
}
}
my test.php model
<?php
class test extends CI_Model {
public function __construct(){
parent::__construct();
}
function echo_print(){
echo 1234;
}
}
her eis the result :
Unable to locate the model you have specified: Test
btw changing it's name to Test wouldn't help and it used to work fine with lowercase before upgrade
According to the version 3 user guide, the class names MUST match the file name, whereas this wasn't the case, in version 2.
http://www.codeigniter.com/userguide3/general/models.html#anatomy-of-a-model
Related
I have a problem with my system when I opened localhost, it displays this error :
An uncaught Exception was encountered
Type: RuntimeException
Message: Unable to locate the model you have specified: Project_model
Filename: C:\xampp\htdocs\txu\system\core\Loader.php
Line Number: 344
Backtrace:
File: C:\xampp\htdocs\txu\application\controllers\home.php
Line: 9
Function: model
File: C:\xampp\htdocs\txu\index.php
Line: 315
Function: require_once
this model: Project_model.php
class Project_model extends CI_Model{
function __construct()
{
parent::__construct();
}
this is controller: Project.php
class Project extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->model('project_model');
}
this is my home controller: home.php
class Home extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->model('Project_model');
}
before this it working, i dont know why it does not working right now. And the console show an error in localhost:
GET http://localhost/txu/ 500 (Internal Server Error)
Can anyone give me suggestion or idea? thank you.
For project module;
Create the controller names as Project.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Project extends CI_Controller {
public function index()
{
$this->load->model('project_model');
echo $this->project_model->my_model_func();
echo "<br> This is the Project Controller";
}
}
Create the model named as Project_Model.php
class Project_model extends CI_Model {
public function my_model_func()
{
echo "Project_model model loaded with my_model_func";;
}
}
And see the output on http://your-base-url/index.php/project
You should see the output as
Project_model model loaded with my_model_func
This is the Project Controller
I hope this will work for you.
In Codeigniter around version 2.1; you instantiate model like below > Note that it starts on upper case
$this->load->model('Project_model');
While on codeigniter version 3+; you always instantiate model starting lower case, or should i say all lower case
$this->load->model('project_model');
Therefore I would like you to check on your codeigniter version if what fits you.
Same on Usage if you instantiate your model like "Project_model", then use it as:
$this->Project_model->action();
it has case sensitivity;
I had a working project uploaded to server 2 week before. It was working fine until now. Below is code for model awsmodel.php
class Awsmodel extends MY_Model {
function __construct()
{
}
// some other functions
}
MY_Model again extends to CI_model.
class MY_Model extends CI_Model {
// required functions
}
From controller pages.php I am calling the function like below.
class Pages extends MY_Controller {
public function index()
{
$data = $this->allCommonMenu();
$this->load->model('Awsmodel');
$data['featured'] = $this->Awsmodel->featuredProp();
$this->load->view('home_pz',$data);
}
}
The above code suddenly stooped working today. After some test and tries, I got to know that when I comment below 2 lines from controller function then The page loads.
$this->load->model('Awsmodel');
$data['featured'] = $this->Awsmodel->featuredProp();
I have changed environment variable to 'development' and tested but still no error message shows. In firefox its showing a blank page, where as in chrome it shows 500 server error. The same code was working since 2 weeks. Don't know why its not working now. If anyone can help me out ?
I am using CI version 2.1.4
If you using CI 3.0 please load model like this
class Pages extends MY_Controller {
public function index()
{
$data = $this->allCommonMenu();
$this->load->model('awsmodel'); //as your model filename: awsmodel.php
$data['featured'] = $this->Awsmodel->featuredProp();
//$data['featured'] = $this->awsmodel->featuredProp();// for CI version 2.1.4 –
$this->load->view('home_pz',$data);
}
}
Have setup a My_Model.php file in application/core/
Contents are:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class My_Model extends CI_Model{
public function __construct() {
parent::__construct();
}
public function x(){
}
}
This is then extended by one of my user Models - loginmodel.php :
<?php
class LoginModel extends My_Model{
public function __construct() {
parent::__construct();
}
function checkAD() {
}
}
All works Fine on my local Windows Bitnami WAMP development deployment but when deployed on the Unix/Apache host.....
getting error:
Fatal error: Class 'My_Model' not found in ..../application/models/loginmodel.php on line 2
httpd.conf issue maybe? Any help?
Thanks
Unix is case sensitive, not Windows. For example you should rename your file loginmodel.php in LoginModel.php, according to your code.
use your application/config file prefix
$config['subclass_prefix'] = 'MY_';
try renaming as
MY_Model
You were all kind-of on the right track. Probably closest to the solution was #wolfgang1983. It was the name of the My_Model.php file. Changed it to MY_Model.php and boom. All the other naming issues seem to be irrelevant - thus far. Thanks
I was using codeigniter 2.2.1. And now codeigniter 3 released. I just tried it and ended up with error.
When I try to load method as in codeigniter2.x, it shows
Unable to locate the model you have specified: Demo
where Demo is my method file.
Controller - welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('demo');
}
public function index() {
$data = $this -> demo ->check();
print_r($data);
}
}
Model - demo.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Demo extends CI_Model {
public function __construct() {
$this->load->database();
}
}
I can't figure-out what is wrong with this code. Please help. Thank you in advance..
Edit:- This is works well in my wamp machine. But I am checking it now in another local machine where my institute host the websites. There it is not working
It was a small issue. I changed model name from demo.php to Demo.php. And it works...
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.