Just started trying out the CI 3 HMVC framework by jlamim (original CI 2 HMVC by wiredesignz): https://github.com/jlamim/ci3-hmvc
Getting Call to undefined method CI_Loader::module()
Here is the referenced code
$data['top_content']['view'] = $this->load->module($module['name']);
Here is the controller code for the module it's calling
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class BatchList extends MX_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
return $this->load->view('BatchList', NULL, TRUE);
}
}
I've read and reread the readme for this version of the framework and I don't know why it won't recognize this method. I tried the alternative Modules::load() and it does not recognize the Modules class.
What did I do wrong?
Related
I'm currently learning Codeigniter. As you know, there is a default file called welcome.php in the controller when you first install the package.
I tried to modify that page to index.php, and here is the code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Index extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
}
}
I also changed the route.php in the config file:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'Index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Then I access the page by entering this path: http://localhost/CI/index.php, but it says there are two errors:
Message: Undefined property: Index::$load. Filename: controllers/Index.php
Message: Call to a member function view() on null. Filename:controllers/Index.php
Did I forget to change something else in order to make it work?
I downloaded CI3.0.2 and tried your code in my computer. I encountered the same problem, and with a few time debug I found what caused this problem.
Your class is Index and your function is index two, in php class when you don't define constructor __construct it will try to find if there's a method that have same name with class nameIndex, so in this situation index function is the constructor of class Index. if this confuse you see this document : constructor php official document
Solution:
class Index extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('welcome_message');
}
}
As I am very pleased of the command show_404() which you can call everywhere to show a 404-Error-Page, I did want to implement a show_403() for requests without permissions.
I created the file application/core/MY_Exceptions.php and added the following code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Exceptions extends CI_Exceptions {
public function __construct()
{
parent::__construct();
}
public function show_403($page = '', $log_error = TRUE)
{
//do some stuff
echo "test";
}
}
Then I'll call it in a controller application/controllers/Welcome.php like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
// show_404(); // <-- this works!
show_403(); // <-- this works not!
}
}
and I keep getting the following error in the browser, when I access the controllers index method:
Fatal error: Call to undefined function show_403()
As you may have noticed I even tested this on a vanilla installation of CodeIgniter, so you should be able to reproduce this error with just these two files.
I know that I could load the extension manually, but that has not the elegance of using show_403() wherever and whenever I want...
Routing is set up correctly, CodeIgniter is version 3.0.3, PHP is version 5.6.12., filesystem permissions to application/core/MY_Exceptions.php have even been set to 777 for debugging purposes.
Your exception class is not loaded, so its giving that error.
Load your class like this
$excep = load_class('Exceptions', 'core', $this->config->item('subclass_prefix'));
echo $excep->show_403();// will echo test
One more suggesion is to use helpers for this,
Add your code in application/helpers with file name error_helper
public function show_403($page = '', $log_error = TRUE)
{
//do some stuff
echo "test";
}
Calling your function
$this->load->helper('error_helper');
echo show_403();
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...
Hi everybody I have a code that give me this error
Fatal error: Class 'MY_Controller' not found in C:\wamp\www\project\application\controllers\admin\home.php on line 3
I have no idea why it's showing this error…
The code of C:\wamp\www\project\application\controllers\admin\home.php is
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends MY_Controller {
function index()
{
redirect('admin/login');
}
function logout()
{
$this->session->unset_userdata('logged_in');
//session_destroy();
redirect('admin/login');
}
}
?>
The code of C:\wamp\www\project\application\libraries\MY_Controller.php is
<?php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
if (!$this->session->userdata('logged_in')) {
redirect('admin/login');
}
}
}
And also if I place
class Home extends CI_Controller
instead of
class Home extends MY_Controller
in the
C:\wamp\www\project\application\controller\admin\home.php
file and try to load the
C:\wamp\www\project\application\libraries\MY_Controller.php
in the constructor of
C:\wamp\www\project\application\controllers\admin\home.php
it shows
Call to a member function userdata() on a non-object
Why so?
You need to put class files to core instead of library folder when you extending System classes. Put MY_Controller.php in core folder.
Refer to the documentation: http://codeigniter.com/user_guide/general/core_classes.html
Core controllers need to be stored in application/core/
So when you extend an object, it will look for it there. Library folder is used for storage of 'external' libraries, which you must explicitly include in your controller:
Ex:
$this->load->library('class name');
Info on libraries here: http://codeigniter.com/user_guide/general/libraries.html
Ok, so in my base controller (page.php) I have the following code which works fine:
$this->load->library('Siteclass');
$mysite = new site_model();
The siteclass library references a model named site_model and instantiates based on data received from that model. All is good.
Now I want to load another library so that I can instantiate another object as well. So I add this to page.php:
$this->load->library('Memberclass');
$mysite = new member_model();
But now I get the following error:
Message: Undefined property: Memberclass::$site_model
Filename: libraries/Loader.php
Line Number: 1035
From what I can tell, it seems that the loader class, when being applied to the Memberclass, is somehow still referencing the site_model instead of the member_model. I've checked my code and I am definitely calling the correct files.
Here's what Siteclass.php looks like:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Siteclass extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Site_model');
$data = $this->Site_model->load_site_data();
// etc etc
and here's what Memberclass.php looks like:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Memberclass extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Member_model');
$data = $this->Member_model->load_member_data();
// etc etc
Thanks in advance for any help!
Gary
I think you're confused about how MVC works in CodeIgniter. Why are you using the loader class to create a controller? Why are you creating a stand-alone instance of your model outside of your controller class?
In CodeIgniter, your URLs represent paths to your controllers' methods. That means that your "base controller" should automatically be instantiated if you go to:
www.example.com/memberclass
Or perhaps more to the point, if you have a link like this:
www.example.com/page
You should have a file in your /application/controllers directory called page.php which looks like this:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Page extends Controller {
function __construct() {
parent::Controller();
// etc etc
Furthermore, unless you're loading data from your model to be used every single time you call this controller, you'll want to put your model calls inside a non-constructor method of this class. Something like:
class Page extends Controller {
function __construct() {
parent::Controller();
}
function index() {
$this->load->model('Member_model');
$data = $this->Member_model->load_member_data();
$this->load->view('myview', array('data'=>$data));
}
}
So again...not entirely sure what context you're doing this all in, but it seems like you're not standing firmly within the framework. There's basically no reason you should be using the loader class to load controllers, and furthermore there's no reason you should be creating stand-alone instances of model classes using PHP's new keyword.