Fatal error: Class not found in codeigniter - php

I have created a model Admin_Model under Application\core directory of code igniter. I put all basic database operations under it. When I try to extend my models who are under Application\model directory, it throws error.
Fatal error: Class 'Admin_Model' not found in <path to root>/application/models/new_model.php on line 3
Should I miss any configuration?

Thanks all for your efforts.
I find a solution.
By default CodeIgniter have a setting in its config file.
$config['subclass_prefix'] = 'MY_';
I just replace 'MY_' with 'Admin_' everything works fine.
$config['subclass_prefix'] = 'Admin_';
More appropriate solution is
Put class file in libraries folder
Add following code to config.php
function __autoload($classname)
{
if(strpos($classname,'CI_') == 0)
{
$file = APPPATH.'libraries/'.$classname.'.php';
if(file_exists($file))
{
#include_once($file);
}
}
}
That's All

This may also be helpful, if you still have problems with __autoload to use class from libraries. it's work for me, when I use class library tobe parent class at controller.
PHP 5 >= 5.1.2, PHP 7
this is mycode at application/config.php
spl_autoload_register(function ($classname){
if(strpos($classname,'CI_') == 0){
$file = APPPATH.'libraries/'.$classname.'.php';
if(file_exists($file)){
#include_once($file);
}
}
});

Extending Core Class
If all you need to do is add some functionality to an existing library - perhaps add a function or two - then it's overkill to replace the entire library with your version. In this case it's better to simply extend the class. Extending a class is nearly identical to replacing a class with a couple exceptions:
The class declaration must extend the parent class.
Your new class name and filename must be prefixed with MY_ (this item is configurable. See below.).
For example, to extend the native Model class you'll create a file named application/core/MY_Model.php, and declare your class with:
class MY_Model extends CI_Model {
}
Note: If you need to use a constructor in your class make sure you extend the parent constructor:
class MY_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
}

I setup my environment on a Windows machine. Then after move to production using Ubuntu, I had the class not found error.
I discovered my files extension were PHP (capital letter). Linux is case sensitive, so the file name need to be lowercase php.

Related

parent controll in Codeigniter 2.2 project not found

In my CI 2.2 project I want to make my parent controll with app's common functionality for use in all app and for this I create file :
application/libraries/N_Controller.php :
<?php
class N_Controller extends Controller
{
public function __construct()
{
parent::__construct();
}
But on first attempt to use it in file
application/controllers/admin/admin.php
<?php
class Admin extends N_Controller
{
public function __construct()
{
parent::__construct();
I got error:
PHP Fatal error: Class 'N_Controller' not found in /controllers/admin/admin.php on line 3, referer: http://local-ci22.com/admin/hostel/edit/15
I tried to add in application/config/autoload.php file :
$autoload['libraries'] = array( 'AppSmarty', 'AppUtils', 'N_Controller');
But it did not help. Which is the correct way ?
if you want to extend the functionality of a system class. you need to follow this recomendations.
place your extended class in /application/core, be sure that you name it exactly like the name and casing of the class created.
your class should extend from CI_Model or CI_Controller, depending on your needs.
wherever you implement your new class, be sure that you honor the same name casing from your extended class.
you should have configured the $config['subclass_prefix'] on /application/config/config.php. let's say in your case with the value 'N_'
what i can see from your code, you are not extending from CI_Controller and your path seems wrong.
Informative note: the /application/library is used to place classes and libraries from 3rd parties that won't fit into CI schemas.

CodeIgniter - is it possible to create my own controller in any other name

I have created custom controller called "MY_Controller.php" in Application/core, and successfully invoked by inheriting through application controller.
//application/core
class MY_AdminController extends CI_Controller {
function __construct() {
parent::__construct();
}
}
//application/controllers
class User extends MY_AdminController {
public function __construct(){
parent::__construct();
}
}
It works fine. I just changed my file name from "MY_Controller.php" to MY_AdminController.php, and following same class name, but it is throwing following error,
Fatal error: Class 'MY_AdminController' not found
As per the documentation, Whenever you create a class with the MY_ prefix the CodeIgniter Loader class will load this after loading the core library, then why its throwing error...!!!
Go to your config.php and change
$config['subclass_prefix'] = 'MY_'; to $config['subclass_prefix'] = 'MY_Admin';
Expanding on Patel,
the issue is that MY_ is the prefix to the original core files.
Controller, Model, View etc.
MY_ will be used to seek the name of the controller, for example, MY_controller searches for CI_controller.
You cannot load random names using the MY_prefix. you use MY_ to extend the already existing names.
I think the problem is with the class name, you may have not changed the class name from MY_Controller to MY_AdminController
You can use custom classes. But CI only loads the classes with the class prefix in the config file (e.g. MY_). So I explained how it works and created a workaround to load classes automatically. You can find it here https://stackoverflow.com/a/22125436/567854.
Hope this helps :)
If you want to include all the files that are in your core folder. Then write the following code in end of config.php file.Path to file is application/config/config.php
function __autoload($class)
{
if (strpos($class, 'CI_') !== 0)
{
#include_once( APPPATH . 'core/' . $class . EXT );
}
}
By using this you can create multiple classes.

Codeigniter Model extending - Fatal error: Class 'MY_Basedbclass' not found in

I know there are other similar questions, but even after following all the syntax, this errror exists
I am using Codeigniter version 2.1.4 on wamp server, windows 7.
in application/core/ i have MY_Basedbclass.php starting with
abstract class MY_Basedbclass extends CI_Model
{
and trying to use it in my mode like
class Accounts_manager extends MY_Basedbclass
{
ending up with the error - Fatal error: Class 'MY_Basedbclass' not found in C:\wamp\www\party_app\server\application\models\accounts_manager.php
Please suggest what i am doing wrong. Thanks
EDIT - Addign a screen shot for the file structure sake
why you define your core model class as abstract class? after that you can only set core model class as MY_Model. this name get from subclass_prefix + class_name_you_want_to_over_write
define your core class like this:
class MY_Model extends CI_Model {
// your code
}
before that assured in your config.php file you set $config['subclass_prefix'] = to MY_
config file address: application > config > config.php

Yii - Inheriting From Custom Controller Class - Not Found

class SomeController extends Controller
{
public function actionIndex() {
echo 'This is some controller';
}
}
class AnotherController extends SomeController
{
public function actionIndex() {
echo 'This is another controller';
}
}
This works:
index.php?r=some
but ...
index.php?r=another
says:
PHP warning
include(SomeController.php): failed to open stream: No such file or directory
Both of the files are in
test\protected\controllers\
BTW in the past I also tried using the Gii Controller Generator with "SomeController" as the base class...
It said:
The controller has been generated successfully. You may try it now.
Generating code using template
"C:\xampp\htdocs\yii\framework\gii\generators\controller\templates\default"...
generated controllers\YetAnotherController.php
generated views\yetAnother\index.php
done!
When I clicked on "try it now" it also said:
PHP warning
include(SomeController.php): failed to open stream: No such file or directory
Edit:
Classes inside protected/controllers are not autoloaded, therefore you'll have to import the parent class file before extending from it:
In AnotherController.php:
Yii::import('application.controllers.SomeController');
public class AnotherController extends SomeController {
// ...
}
Incase you need to access the base class from url also, you can use the above method. Otherwise you can put your base class inside protected/components as you have already figured out.
Yii autoloading works only when you have the same name for the file as the class that the file contains. Meaning class SomeController should be within SomeController.php file.
Make those changes and it should work.
A helpful wiki: Understanding Autoloading Helper Classes and Helper functions.
Guide link:
Class files should be named after the public class they contain.
To extend any class just go to the config file and add the class in the import section
'import' => array('application.controllers.SomeController')
this will make it available in the entire application without importing explicitly.

Zend - plugin not found in the registry

I've been researching about this for hours, but in vain... I have a Zend application and these are two classes from it:
class Application_Form_Disciplines extends Zend_Form {
public function init() {
$this->addElementPrefixPath('My_Validate', '../library/validate', 'validate');
...
$credits->addValidator('My_Validate_NumericBetween');
...
}
}
class My_Validate_NumericBetween extends Zend_Validate_Abstract
{
...
}
The problem is that when i submit the form i get "Plugin by name 'My_Validate_NumericBetween' was not found in the registry; used paths: My_Validate_: ../library/validate/ Zend_Validate_: Zend/Validate/". The class named "My_Validate_NumericBetween" is found in project/library/validate. I've tried a lot of things found on the net, but nothing worked.
Thank you!
Try:
$this->addElementPrefixPath('My_Validate', APPLICATION_PATH . '/../library/validate', 'validate');
$credits->addValidator('NumericBetween');
Side note: Typical PSR-0 classname/file scheme would be to have the class My_Validate_NumericBetween stored in the file library/My/Validate/NumericBetween.php, rather than library/validate/NumericBetween.php (which I infer you are using).
Put this in Bootstrap.php, it will save you some typing in the future too
Zend_Loader_Autoloader::getInstance()->registerNamespace('My_');
rename the file to NumericBetween.php
the class name should remain My_Validate_NumericBetween

Categories