This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am trying to extend the CodeIgniter controller. I have created MY_Controller.php file, the content of the file is as following:
<?php
class MY_Controller extends CI_Controller
{
function generate_key()
{
$n = rand(10e16, 10e20);
return base_convert($n, 10, 36);
}
Now I create my controllers by extending MY_Controller instead of CI_Controller. Following is an example of a controller:
class Member extends MY_Controller
{
public function index()
{
$this->load->view('welcome');
}
I have placed the MY_Controller.php file in the Application/libraries/ folder. But when I load the application, I get the error:
Fatal error: Class 'MY_Controller' not found in path\to\application\controllers\member.php
Can someone tell me what i am doing wrong? Thanks.
Edit: I'm using CodeIgniter 2.0.2
I had been this issue:
`Fatal error: Class 'MY_Controller' not found ...`
publishing to my remote Linux server (DreamHost) but not on locally on Windows. The issue was Linux being case sensitive: I had MY_Controller.php with a lowecase c and it wasn't autoloading.
Just in case anyone else has the same issue :)
You need to create MY_Controller.php in application/core. I tried what you have setup and got the same problem. Moving the custom controller to core solved the problem.
Hope it helps!
I had problem like this,After some search I found error was made myself,Because my controller class name was MY_Controller but file name was My_Controller[Case not matching]. Note:- In localhost I didnt have any error.
In extended controller I Use
class Home extends MY_Controller{
function __construct() {
parent::__construct();
}
}
even I got the error.
After changing my file name to MY_Controller it started to work well.
MY_contrller places in application/core folder.
Related
It is working correctly with MX_Controller.It is throwing error as 'Fatal error: Class 'CI_Template' not found' when using REST_Controller.Any help will be appreciated.
I'm not entirely familiar with the inner workings of Codeigniter's third party add-on HMVC. However, the code base that I inherited is using it.
I ran in to the same issue when installing restserver, https://github.com/chriskacerguis/codeigniter-restserver which is what I assume you were also doing.
REST_Controller.php has this
abstract class REST_Controller extends \CI_Controller {
While I have never been able to get CI_Controller to work with this codebase, I now believe it is because of this HMVC. For those not familiar, HMVC is Hierarchical Model View Controller.
Module Controllers can be used as normal Controllers or HMVC
Controllers and they can be used as widgets to help you build view
partials.
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
I did get REST_Controller.php working by changing the line to
abstract class REST_Controller extends \MX_Controller { // \CI_Controller {
My model is:
/application/models/tester.php
The code inside is:
class Tester extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}
}
And here's the usage:
class Hauth extends CI_Controller
{
public function index()
{
$this->load->view('hauth/home');
}
public function login($provider)
{
// Load models
$this->load->model('Tester');
// Load helpers
$this->load->helper('url');
log_message('debug', "controllers.HAuth.login($provider) called");
It works perfectly on my local WAMP server but I get this error on the LAMP production envrionment:
An Error Was Encountered
Unable to locate the model you have specified: Tester
I've looked at a dozen StackOverflow questions that appear to be similar but I've tried all of the solutions posted. As far as I can tell, I've followed the CI manual to the letter. I figured that perhaps file names were an issue WAMP vs LAMP so I confirmed the case of the file names via FTP. Thought it might be a naming conflict, so I renamed them "tester". Again, works perfect on local, but not on live.
I found the problem. Turns out I accidentally used a zip of Codeigniter 3.0-dev. When I moved my application files over to the latest stable version, everything instantly worked.
I'm very new to the Laravel framework and am trying to load a simple controller in my browser to slowly get the hang of things.
I have a file that's titled users.php inside of the the laravel/app/controllers/ folder and it looks like this:
class UsersController extends BaseController
{
public $restful = true;
public function action_index()
{
echo 'hi';
}
}
In the routes.php file, I have
Route::get('users', 'UsersController#index');
But, when I go to
http://localhost:8888/laravel/public/users
I'm greeted with a message that says "ReflectionException
Class UsersController does not exist"
I'm not sure if this is because I didn't install the mcrypt extension of PHP. But, when I checked the php.ini file on MAMP, it said that it was enabled. Upon entering
which PHP
in my terminal, it said /usr/bin/php. So, it might not be using the correct version of PHP.
I'm not entirely sure if this is a routes problem or if it's stemming from an absence of a vital PHP extension.
Thanks a bunch!
You need to use the Route::controller method to reference your Controller:
Route::controller('test', 'TestController');
...and rename your file (as Cryode mentions ) to be TestController.php.
Note - if you want to use the filename as test.php, then you will need to use composer to update the autoload settings.
Finally, the format of names for Controller methods changed in Laravel 4, try renaming the method
public function action_index() {}
to be
public function getIndex() {}
the get represents a HTTP GET request... the same applies for post (HTTP POST) and any (GET or POST.. )
I'm not familiar with that part of Laravel's source, so I'm not entirely certain that this is the issue, but your controller file name should match the controller class name, including capitalization.
So users.php should be UsersController.php. Now, when I do this myself on purpose, I get a "No such file or directory" error on an include() call, so that's why I'm not certain that's the sole cause of your problem. But it may be a start.
I am working with codeigniter in php and my question is that How can i Call an external controller in a controller located in controllers folder.
//this file is Controller2.php in controllers folder
class Controller2 extends CI_Controller
{
function one()
{
// Some code goes here..
}
}
//and now this file is Controller1.php which is also in controllers folder
class Controller1 extends CI_Controller
{
function one()
{
// I want to load Controller2 here
}
}
Please instruct me as i've wasted so much time during googling.
Thanks in advance..
Kamran
You can, but not with the default functionality, to use something similar you should use HMVC extension for CodeIgniter: Link
There are plenty examples and a good thread about the extension.
You can't, or rather you shouldn't. Any functionality that you need to use in two different controllers should be moved to a library or helper file.
I am facing a Fatal error:
Class 'Controller' not found in D:\wamp\www\CodeIgniter\application\controllers\blog.php on line 3
I am typing write and i watched so many tutorials they are also typing beginning code like this i tried a lot to find out but got no reason
Are you using CI 2.O? Did you by any chance write the following in your controller:
class Blog extends Controller { ... }
If you're using CI 2.0, you should use the following:
class Blog extends CI_Controller { ... }
You might have been watching outdated tutorials for CI 1.7.
Reference: http://codeigniter.com/user_guide/general/controllers.html
Make sure to follow the userguide along with whatever tutorials you're following; there's some changes in 2.0 which you should be aware of. You can still follow these tutorials though, just keep your eyes open and compare with the userguide.
What version are you using? if you're using the latest (2.0.2), make sure you use CI_Controller when extending your controller.
Seeing you named the file blog.php, your controller should be looking like this
Class Blog extends CI_Controller {
function index()
{
// your code...
}
}