I have created under application/core folder a file called Auth_controller.php.
I am using CodeIgniter 3.
php 5.3.3
Centos 6.9
The content is
class Auth_Controller extends CI_Controller{
in config file i declare
$config['subclass_prefix'] = 'Auth_';
Then finally when i called it...
class Home extends Auth_Controller {
it gives me a
Fatal error: Class 'Auth_Controller' not found in /var/www/html/calllist/application/controllers/Home.php on line 3
Message: Class 'Auth_Controller' not found
You using $config['subclass_prefix'] = 'Auth_'; settings means you want to extend core CI classes.
If you want to extend CI_Controller then you class name should be Auth_Controller if you want to extend model your class name should be 'Auth_Model'
But Remember those classes/files should be inside your application\core folder
For more details please look at CI documentation
The location of Auth_Controller must be in application\core directory. Otherwise you need to manually include class to your controller file.
You need to include Auth_Controller like below mentioned code.
<?php
require_once("secure_area.php");
class Reports extends Secure_area {
Related
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.
I can't figure out what I'm doing wrong. In the core directory of my app I have two files CMController.php and CMBase.php.
CMBase.php
class CMBase extends CI_Controller {}
CMController.php
CMController extends CMBase {}
Then in random controller
class RandomController extends CMController {}
And I get
Fatal error: Class 'CMBase' not found
subclass_prefix in the config is set to CM.
If I do require_once 'CMBase.php' in CMController file it works fine.
So my question is why the file CMBase is not loaded automaticly?
When you are extending the core controller your class name needs to end with "Controller". So you can't name your class CMBase it would have to be CMBase_Controller.
So if the value in $config['subclass_prefix'] is "CM" you class needs to look like
class CMController extends CI_Controller {//...}
Then once you have done that you can do
class AnyClassName extends CMController {//...}
Have you tried looking $config['subclass_prefix'] = 'MY_'; applications folder. Can only extend MY_Controller etc. http://ellislab.com/codeigniter/user-guide/general/controllers.html
https://github.com/EllisLab/CodeIgniter/wiki/MY-Controller
I think your missing the _ underscore part ie MY_
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.
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
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.