I have some codeigniter application. It works well on my local server. After I uploaded it to the hosting server it won't work and resulting an error:
Fatal error: Class 'System_Controller' not found in /home/k2113138/public_html/test/application/controllers/login.php on line 3
Here is my System Controller that extending CI_Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//location: application/core/
class System_Controller extends CI_Controller {
public function __construct(){
parent::__construct();
//some code
}
}
my controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//location: application/controllers/
class Login extends System_Controller {
public function __construct()
{
parent::__construct();
}
}
i have set my config as my desired configuration like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['base_url'] = 'http://xx.com/subfolder/';
$config['subclass_prefix'] = 'System_';
?>
I have read many article that discuss this matter but still cannot found the solution. Really confuse about the problem, because everything works well in local server. I have configured the config file as what it's needed like the database and the routes configuration. Is there any other things that i need to re-configure?
EDIT: My Codeigniter version is 2.1.3
Ok, you changed the filename to lowercase, now, try change the classname too:
class system_controller extends CI_Controller
If don't run, play with the classname and the filename, this is a problem of your server, very common in shared servers.
Sometimes, the PHP version and its configuration, play a important rol in this cases.
Related
I have a problem with namespaces. Follow the code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class NavBar extends fwportal\controllers\template\NavBar {
function __construct()
{
var_dump('navBarPortal');
parent::__construct();
}
}
And the main class:
<?php
namespace fwportal\controllers\template;
use fwportal\controllers\NavbarPermissoes;
if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
Abstract class NavBar extends \CI_Controller
{}
This return the error below:
Fatal error: Class 'fwportal\controllers\template\NavBar' not found in /var/www/portalsibe/sistema/controllers/template/NavBar.php on line 6
Anyone can help me with this?
I don't know why this error occur, because I used in other files with the same mode and works ok.
If you are using Codeigniter 3 then most probably you can not extend "\CI_Controller" while you are defining namespace on a class.
May be this is the reason of getting error.
I am using Ashimante Rest Server API in my project.
This is what I did:-
I have put the Rest.php controller in the application/libraries
folder
I have created webservice-v1 folder inside the
application/controllers/ folder i.e. application/controllers/webservice-v1
I have Controller files rest_api_common.php and
webservice_test.php inside the webservice-v1 folder.
This is my rest_api_common.php code:-
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require(APPPATH . 'libraries/Rest.php');
class Rest_api_common extends REST {
// some variable declarations //
public function __construct() {
parent::__construct();
// loading models and custom config files//
}
}
I want the variables and functions available to all the webservice functions. So my webservice_test.php file code goes like this:-
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include('rest_api_common.php');
class Webservice_test extends Rest_api_common
{
function __construct(){
parent::__construct();
}
public function mongodata_post()
{
$this->response(array(),200);
}
}
I have defined route like this:-
$route['rest-api/v1/mongodata'] = "webservice-v1/webservice_test/mongodata_post";
Using postman app, I am calling the API like this using POST method:-
http://testdomain.com/rest-api/v1/mongodata
I am getting this response:-
{"status":false,"error":false}
What am I doing wrong?
I was using codeigniter 2.2.1. And now codeigniter 3 released. I just tried it and ended up with error.
When I try to load method as in codeigniter2.x, it shows
Unable to locate the model you have specified: Demo
where Demo is my method file.
Controller - welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('demo');
}
public function index() {
$data = $this -> demo ->check();
print_r($data);
}
}
Model - demo.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Demo extends CI_Model {
public function __construct() {
$this->load->database();
}
}
I can't figure-out what is wrong with this code. Please help. Thank you in advance..
Edit:- This is works well in my wamp machine. But I am checking it now in another local machine where my institute host the websites. There it is not working
It was a small issue. I changed model name from demo.php to Demo.php. And it works...
I am using WireDesignz modular extensions with great success so far. I now have the need to extend a controller within the module. I have created the new controller and the original, now extended, controller and they work fine outside of the HMVC but when I put them inside the module folder and call the new controller it days is can't find the controller it's extending... even though it's right there in the same directory. If I call the original one it's all fine. I'm not sure where to go with this as I can't find anyone with the same problem online. Any ideas? Here's a bit more:
Original controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Calendar extends MY_Controller {...
New controller, in same directory:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Calendar_new extends Calendar {...
Results in:
Fatal error: Class 'Calendar' not found in /home/d/e/demo/web/public_html/application/modules/calendar/controllers/calendar_new.php on line 2
Thanks.
The base controller class which you are extending is not being included as a resource. Codeigniter will not automatically try to load base classes.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include('Calendar.php');
class Calendar_new extends Calendar {...
I am unable to load my custom class which doesnot extend from any core class.
I have placed my custom class in a subfolder inside application/libraries.
So here is my folder structure
application
|_ libraries
|_ cgh
|_ cgh_asset.php
|_ cgh_article.php
|_ cgh_asettype.php
|_ controllers
|_ welcome.php
Class Cgh_article is a subclass of Cgh_asset
Cgh_asset.php :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
abstract class Cgh_asset
{
public $id;
public $type;
public $title;
public $content;
public $user;
abstract public function generate_url();
function __construct()
{
$this->generate_url();
}
}
?>
Cgh_article.php :
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cgh_article extends Cgh_asset
{
function __construct()
{
parent::__construct();
$this->type=Cgh_assettype::article;
}
function generate_url()
{
$this->url="Article_URL";
}
}
?>
Cgh_assettype.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cgh_assettype
{
const type1="type1";
const type2="type2";
const article="article";
}
?>
Controller welcome.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->library('cgh/Cgh_assettype','cgh/Cgh_asset','cgh/Cgh_article');
$this->load->view('welcome_message');
}
}
The error I get is :
Unable to load the requested class: Cgh_assettype
I must have tried all possible upper and lower case combinations of classnames, filenames, but the error is always the same.
After going through some answers, I think probably I should add one basic question here - Is it at all possible to have my own custom object types within codeigniter ... types that should be quite obvious from my question ?
This seems to work for me, so here is what I will be doing ... at least till something breaks :
In the constructor of my controller, I use require_once for my classes ... and good thing is I can combine all my classes into a single file -- my classes initially were in a single file anyways --This is my controller after the changes, and this works :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public $cgh_assettype;
public $cgh_asset;
public $cgh_article;
function __construct()
{
parent::__construct();
//$this->load->library(array('cgh/cgh_assettype','cgh/cgh_asset','cgh/cgh_article'));
echo "Including CGH<br />";
echo "<p>Apppath is ". APPPATH. "</p>";
require_once(APPPATH.'libraries/cgh/Cgh_assettype.php');
require_once(APPPATH.'libraries/cgh/Cgh_asset.php');
require_once(APPPATH.'libraries/cgh/Cgh_article.php');
}
public function index()
{
$iCgh_article=new Cgh_article();
echo "<p>$iCgh_article->url</p>";
$this->load->view('welcome_message');
}
}
You need to call $this->load->library for each library.
$this->load->library('cgh/Cgh_assettype');
$this->load->library('cgh/Cgh_asset');
$this->load->library('cgh/Cgh_article');
$this->load->library takes 3 parameters.
File to load
(optional) $config array
(optional) String to rename library to ($this->Renamed_library)
If you want to load multiple libraries on one line, use an array as the 1st parameter.
$this->load->library(array('cgh/Cgh_assettype','cgh/Cgh_asset','cgh/Cgh_article'));
Are you library file names capitalized? (Your submitted folder structure says they are not).
I don't know about having libraries within sub-directories, but I know the file names need to be capitalized.
http://codeigniter.com/user_guide/general/creating_libraries.html
Naming Conventions
File names must be capitalized. For example: Myclass.php
Class declarations must be capitalized. For example: class Myclass
Class names and file names must match.
You should load library or whatever only once. If you're loading for second time, you get that error.