In my web app I'm extending the Codeigniter core using applicaiton/core/MY_controller as below:
class MY_Controller extends CI_Controller {
function __construct(){
parent::__construct();
}
}
And in my controller I have:
class Dashboard extends MY_Controller {
public function __construct(){
parent::__construct();
// Your own constructor code
}
public function index(){
}
}
This work perfectly on my localhost, however on my production server it returns a 500 error with an error message of "PHP Fatal error: Class 'MY_Controller' not found...".
PHP version on localhost is 5.4.10 and on production is 5.4.15.
I'm using the latest version of Codeigniter.
Thanks in advance!
You're developing under Windows (which is case-insensitive) and deploying on a UNIX-based host (which is case-sensitive). Rename MY_controller.php to MY_Controller.php, with a capital C.
Related
I am using MAMP to locally host my codeigniter project. Each of my controllers extends MY_Controller. MY_Controller looks like the following:
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('user_model');
$this->user_model->do_something();
}
}
Using MAMP, on my macbook, this works fine. However, when I upload my site to my linux server running apache, I get the following error:
Unable to locate the model you have specified: User_model
Why?
UPDATE
I changed the capitalization to be like this:
$this->load->model('User_model');
$this->User_model->do_something();
and the problem continues
Linux is a case-sensitive operating system, and as such you need to be really careful when developing on Windows and deploying to Linux.
Your issue stems from the line :
$this->user_model->do_something();
Which should be :
$this->User_model->do_something();
As it is the name of an object you are trying to access.
The line which loads it is fine, as CodeIgniter doesn't mind which case you use to load the models, but PHP will get somewhat picky on Linux when it comes to names of things.
I am facing a weird problem, the below function works fine when I was testing it in a Windows system, but the same function is unable to the load class when I am using it in Ubuntu 12.04.
Below in my function, the commented include loads the function , but spl_autoload() does not load the class. I am confused as to what might be the reason.
public function controller($class)
{
$class = preg_replace('/Controller$/ui','',$class);
ini_get('include_path');
set_include_path(CONTROLLER_DIR.lcfirst($class).DIRECTORY_SEPARATOR);
spl_autoload_extensions('Controller.php');
#include(get_include_path().$class.spl_autoload_extensions()); # <-----This works fine
spl_autoload($class); # <----but this is not loading the class
}
Any thoughts?..
I have looked for this answer all through stackoverflow and none have been able to help me.
my file name is: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
}
}
I made a post in this thread asking if anyone had found an answer. I tried everything in that thread, and in all suggested links.
I'm at a complete loss.
Everything works on my local WAMP server (apache 2.4 php 5.4) and not on the production server (Ubuntu 12.04, apache 2.4, php 5.5)
error:
PHP Fatal error: Class 'MY_Controller' not found in filepath/application/controllers/welcome.php on line 7.
Line 7 is where I define the class: class welcome extends MY_Controller {
EDIT
Thanks for all the help. I figured out what was wrong.
When I initially started trying to figure out this problem, I noticed that I did not have my case right on the name of MY_Controller.php, it was My_Controller.php.
So, what I found out was that even though I changed the name of the file on my local machine, when I uploaded it, the name still didn't change. So, when I went to change it to all lower case I decided to do that directly on the production server and found that after all this time it was still named with the lowercase y when I thought I had changed that. I hope this helps anyone else who migrates from a WAMP environment to a LAMP environment to know that even though the case is changed, it is still the same name, and may or may not be changed when you upload it.
please go to your application/config/config.php and on the bottom insert this code
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
#include_once( APPPATH . 'core/'. $class . EXT );
}
}
Now you are good to go.
please try creating file MY_Controller.php in /core folder with this body
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->output->enable_profiler(TRUE);
}
}
and use welcome controller if it works.
I missed note: Everything works on my local WAMP server (apache 2.4 php 5.4) and not on the production server (Ubuntu 12.04, apache 2.4, php 5.5)
Please check your case of files/controllers
Please try editing/renaming everything in to lower case (even my_controller extends CI_Controller).
I moved cakephp installation to a different host and got this error(the code segment is the output I get including the php code, it is suggesting AppController would extend AppController):
Missing Method in AppController
Error: The action index is not defined in controller AppController
Error: Create AppController::index() in file: app/Controller/AppController.php.
<?php
class AppController extends AppController {
public function index() {
}
}
My database is now located on a different host altogether and I tested the remote connection and cake doesn't complain about that anymore. Any suggestions what could've caused this?
I really need some guidance here as this issue is pretty confusing and extremely different to understand for a beginner like myself.
I am running a WAMP server with the latest CI version.
In core/MY_Controller.php I have:
public GeneralController extend CI_Controller {
public GeneralController() {
parent::__construct();
// Does some stuff
}
}
public AuthenticatedController extend GeneralController {
public AuthenticatedController() {
parent::__construct();
if(!loggedIn()) redirect("/login");
// Does some stuff
}
}
public UnauthenticatedController extend GeneralController {
public UnauthenticatedController() {
parent::__construct();
if(loggedIn()) redirect("/home");
// Does some stuff
}
}
My login controller is:
class Login extends UnauthenticatedController {
So basically if they are logged in and load "/login" they will be routed to "/home".
This works perfectly on my local environment.
Once I upload it to my server and navigate to "/login" I get an infinite loop. After debugging I figured out that the Login controller loads AuthenticatedController instead of UnauthenticatedController so it keeps redirecting back to "/login" infinitely.
Well, now the inheritance is broken for some reason so I need to check if it calls both Auth and Unauth controllers. Nope. Just calls Auth even though it extends UnauthenticatedController.
I am at a loss here, I've tried everything I can imagine but as a new php programmer I thought I would pick some of your brains on things to try!
Thank you!
Solution:
Check your production server php version vs. local
I also had this problem with extending core classes. My problem was that is was working on my local server, but not on my production server. I was receiving the 404 error in production, and only on my controllers that used the class extensions. After some research I noticed that my local server was running php version 5.3.x, while my production server was running 5.2.x. To fix this I had to upgrade my production server to 5.3.
To find out what version of php your site is using, place this command in your view:
<?php echo phpversion() ?>