php spl_autoload not loading the class - php

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?..

Related

Codeigniter site working in MAMP but not LAMP

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.

php 5.5.9 gives error when subclass is declared before parent

in my installation of php 5.5.9 on ubuntu 14.04 server, I get error when subclass is declared before parent.
testing.class.php
<?php
class mysubclass extends parentclass
{
}
class parentclass
{
}
myheader.inc.php
<?php
// empty header
index.php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once 'myheader.inc.php';
require_once 'testing.class.php';
echo "testing";
I get the following error
Fatal error: Cannot redeclare class mysubclass in /var/www/clients/client1/web3/web/central/try/php559/testing.class.php on line 4
when I replace the places of parent and subclass in testing.class.php I do not get the error.
The only possible explanation for this is that you have set up an autoloader. When PHP hits extends parentclass, it autoloads the file again to get its implementation, which also reloads mysubclass, which leads to the error.
You need to include/define/load parent classes before subclasses, there's no other way. Preferably every class should be defined in its own file as well.
after months, I found the source of the problem; it is not PHP, it is xcache. Since PHP 5.5 includes opcache, I was running both xcache and opcache on server. I uninstalled xcache and problem disappeared. I don't know which one is better, I prefer to use what comes with PHP.

How can I get CodeIgniter 2.1.4 to load my extended Controller Class?

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).

CodeIgniter - Controller Double Inheritence Breaking When Deploying To Server

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() ?>

Fatal error: Undefined class constant 'EXCEPTION_NO_ROUTE' in Zend framework

I have downloaded Zend latest version(Zend Framework 1.11), then created the project using command line tool.
Also I've created controller in the created project. In the created controller Just I've added new method,
<?php
class UserController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// action body
}
public function chatAction()
{
echo 'Test';
}
}
then tried to access in the browser
http://localhost/user/chat
I got the following error
Fatal error: Undefined class constant 'EXCEPTION_NO_ROUTE' in F:\xampp\htdocs\z_app\application\controllers\ErrorController.php on line 16
Thanks in advance.
You probably have the wrong zend library included.
Check your version via echo Zend_Version::VERSION; in index.php and die() before bootstrapping (as your mvc is broken).
You may need to update your include path to the correct location of the 1.11 zend framework library. Probably you have multiple versions (i.e. zend studio comes with its own) installed.
Thank you friends, Finally I resolved the issue by just copying the Zend directory inside of the library of Zend framework directory to my project directory. Now it is working.
F:\xampp\htdocs\zend\library
To
F:\xampp\htdocs\z_app\library
Even though I've added the include path in the php.ini to the zend framework library directory like below,
include_path = ".;F:\xampp\php\PEAR;F:\xampp\htdocs\zend\library
It wasn't working.

Categories