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 {...
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 need your help, please!
I have 2 models with the same name
1.... application/admin/user/models/User_model.php, with this code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends CI_Model
?>
2.... application/front/user/models/User_model.php, with this code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends CI_Model
?>
Both classes without differences.
I need to use application/admin/user/models/User_model.php
from application/front/user/controllers/User.php
I tried to do the following changes:
If I add in application/front/user/models/User_model.php the namespace like this code:
<?php namespace customers;
defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends \CI_Model
<?
And from application/front/user/controllers/User.php, I call application/admin/user/models/User_model.php always gives me an error as application/front/user/models/User_model.php was not loaded.
If I delete the namespace from application/front/user/models/User_model.php my code always goes to this mode NEVER to admin/user/user_model.
I need to AVOID change admin/user/User_model.php because a lot of classes uses this MODEL.I only can change front/user/User_model.php
But I need to USE admin/user/user_model.
I call application/admin/user/models/User_model.php like this:
$this->load->model('user_model');
I need to load application/admin/user/models/User_model.php
Please help me!!!!
Thx!
Ani
That is simply not possible, since Codeigniter doesn't support Namespaces
Usually you would see an error like
Fatal error: Cannot declare class `User_model`, because the name is already in use
But Codeigniter prevents this, because it doesn't load any models which are already loaded. You can see this here.
So the only way out of your misery is to simply rename your models - even if you don't want to ;)
Hope this helps you
You can created subdirectory in models like this :
application/models/admin/User_model.php
application/models/user/User_model2.php
Load the model like this:
$this->load->model('admin/User_model');
$this->load->model('user/User_model2');
you should assigned your model to a different object name you can specify it via the second parameter of the loading method:
Use them like this :
$this->load->model('admin/User_model', 'admin_model');
$this->admin_model->some_method();
$this->load->model('user/User_model2', 'user_model');
$this->user_model->some_method();
For more : https://codeigniter.com/user_guide/general/models.html
My library Class in common/auth/auth_manager.php folder :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Auth_manager {
public function __construct() {
$this->allow_dev_login = TRUE;
$this->_ci =& get_instance();
$this->_ci->load->spark('flexi-auth/1.5.0/');
}}
My controller :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
error_reporting(E_ALL);
class Contacts extends MY_Controller {
public function __construct() {
parent::__construct();
echo "string";
$this->load->library('auth/auth_manager');
}
In the above code the string before loading the library is working . But after loading the page is just blank . Have to use the functions from those libraries . If i use the below code
$this->auth_manager->register();
Getting the error property not defined .
your library file at wrong place
According to Codeigniter standard Keep your library in application/libraries/ directory then you can load library
$this->load->library('auth_manager');
For more follow here
or official docs :- https://ellislab.com/codeigniter/user-guide/general/creating_libraries.html
$this->load->library(); method is finding library in /application/libraries or /system/libraries folders, if you want to load library from another location then register third party folder path
$this->load->add_package_path(APPPATH.'common/auth/');
and then user load library method
$this->load->library('auth_manager');
I have been using codeigniter for sometime now. But I am still new to it. And when I bump into some problem, its takes days to figure it out and solve it.
Unable to locate the model you have specified: page_m
I usullay face these sort of errors too. However since I am using multiple hierarchy of classes (Controllers) I am not being able to figure it out.
Is there any way I could make codeigniter to log the error messages with the line number??
You have to create model in Application/Models directory. and make sure you extend your CI_Model class.
eg. model name : my_model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class my_model extends CI_Model {
public function __construct() {
$this->load->database();
}
}
Are you sure that the file name is the same as the name of the class?
If your file is page_model.php the class must start with class Page_Model extends CI_Model
And in your controller remember $this->load->model('page_model');
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.