Codeigniter: Error in loading HMVC sub module model - php

I am having trouble loading model in sub module controller from sub module itself.
I am having few modules and sub-modules as below
modules/admin/
modules/admin/models
modules/admin/controllers
modules/admin/views
modules/admin/models/dashboard/
modules/admin/controllers/dashboard/
modules/admin/views/dashboard/
modules/admin/models/plugs/
modules/admin/controllers/plugs/
modules/admin/views/plugs/
Each M/V/C has own files in it.
Now I have created model in modules/admin/models/plugs/ just for testing purpose something like below
Plugs Model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Plugs extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function mymeta()
{
return 'plugs model loded';
}
}
And now trying to load into Plugs Controller as below
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
class Plugs extends MX_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('plugs');
}
public function index()
{
$this->load->view('plugs/index');
}
public function get_plugin_meta()
{
echo $this->plugs->mymeta(); // this is the method from Plugs Models
}
}
But when I tried to access the URL http://localhost/mysite/admin/plugs/get_plugin_meta or http://localhost/mysite/admin/plugs it is giving me below error.
An Error Was Encountered
Unable to locate the model you have specified: plugs
So than how to load model in controller?

Your module is not plugs. It's admin because of modules/admin
You should use like $this->load->model('module/model');
Try this one:
$this->load->model('admin/plugs/plugs'); // module/folder/file
Important
Controller class name and model class name was conflicting.
Just rename model filename with: plugs_model.php
and change class name with: class Plugs_model extends CI_Model
$this->load->model('admin/plugs/plugs_model');
echo $this->plugs_model->mymeta(); // plugs model loded

Related

Load a model in Codeigniter 3.0.3

I have a project whith this structure in controllers folder:
places.php
users.php
items.php
...
And in my models folder:
Place.php (the name inside is class Place extends ActiveRecord\Model)
User.php
...
Inside my controller places.php if I want to load a model I have to do just that:
$this->load->model('Place');
And after that I have to call my method like that:
$this->place->get_all_places();
It was work in my localhost but not in my server I check my php version in the server and it's 5.6.
How I can fix that?
That's my model file Place.php
class Place extends ActiveRecord\Model
{
static $table_name = 'places';
static $has_many = array(
);
public function get_all_places()
{
return true;
}
}
That's my controller file places.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Places extends MY_Controller {
function __construct()
{
parent::__construct();
if($this->user){
$access = TRUE;
}else{
redirect('login');
}
}
function index()
{
$this->content_view = 'places/all';
}
function get_places(){
$this->theme_view = '';
$this->load->model('Place');
$this->place->get_all_places();
}
}
And the error it was that:
Unable to locate the model you have specified: place
Your model file name should be
Places_model.php
and inside model
class Places_Model extends CI_Model # Changed
{
static $table_name = 'places';
static $has_many = array();
public function get_all_places()
{
return true;
}
}
Models in Codeigniter
I think every thing is right.But the problem is due to this line..
class Place extends ActiveRecord\Model
If you define a new model which extends the CI_Model.And then name of new model can not like as ActiveRecord\Model.Name may be as ActiveRecordModel.SO one solution for you can be..replace above line as below if you define the new model named as ActiveRecordModel in application/core folder.
class Place extends ActiveRecordModel
Another solution can be if you have not define any new model.then just replace above line as
class Place extends CI_Model

Access subclass in codeigniter 3.0.1

I will extend the controller based of login typical, so I create on application directory like this:
core
-MY_Controller
-public controller
MY_Controller
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
}
public controller
<?php
class Public_Controller extends MY_Controller
{
//Some Logic here
public $layout = 'layout';
}
Now, time to use those things.
I write on application/route $route['default_controller'] = 'Home';
So, the controll that named Home would be like this :
<?php
class Home extends Public_Controller {
public function index() {
$this->load->view('public/home');
}
}
But unfortunately, it gives me error like this :
Fatal error: Class 'Public_Controller' not found in C:\wamp\www\egi\application\controllers\Home.php on line 5
Why I can not to extends the sub class ?
NOTE
But if I extends from MY_Controller, its success
<?php
class Home extends MY_Controller {
public function index() {
$this->load->view('public/home');
}
}
Any help it so appreciated
You can do this in one of the following ways.
Have a file named Public_controller in "application/core" & include that file in your application/core/MY_Controller.php file.
Define Public_Controller inside MY_Controller file which is autoloaded by default which means you'd end up with something like this.
File: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
public function __construct () {
parent::__construct();
}
}
class Public_Controller extends CI_Controller {
public function __construct () {
parent::__construct();
}
}

CodeIgniter can't call function from model in controller

I am fairly new to Codeigniter and am trying to call in a function from my model but I cannot get it to work. Can anyone see what I am doing wrong here?
Controller (farm.php):
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Farm extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('harvest_first');
}
function harvest()
{
echo $this->harvest_first->harvest();
}
}
Model (harvest_first.php):
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Harvest_first extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function harvest(){
return "THE FUNCTION";
}
}
?>
I am trying to echo "THE FUNCTION", but no matter what I do I cannot get it to work as expected.
Thanks,
Simon
Try this
class Harvest_first extends CI_Model
change to :
class Harvest_first_model extends CI_Model
and in controller call like this:
$this->load->model('harvest_first_model');
and
$this->harvest_first_model->harvest();
Check here
There is no need to add "_model" to model that depends on you
just Load the Model and that its use autoload.php and add model there it is a good pratices

CodeIgniter 'MY_' can not be found in ... error

I have been working on a session validation for my login to make sure that a user is logged in to view pages. I keep getting this error:
Fatal error: Class 'MY_Staffcontroller' not found in /usr/local/var/www/CodeTest
/ci/application/controllers/staff_c.php on line 3
My staff_c page looks like so :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Staff_c extends MY_Staffcontroller {
function homepage()
{
$data['main_content'] = 'homepage_view';
$this->load->view('includes/template', $data);
}
}
I have been reading same questions all over the place and they say the same thing pretty much...
Is your controller located in application/core?
Well yes it is. I can't seem to get passed this hump!
This is the code within My_Staffcontroller.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_staffcontroller extends CI_Controller {
function __construct()
{
parent::__construct();
$loggedin = $this->session->userdata('loggedin');
if(!isset($loggedin) || $loggedin != TRUE);
{
die($this->load->view('denied'));
}
}
}
I know this is user error as this is only my second day with CodeIgniter but I can't seem to find proper workaround for this?
I have tried this tutorial and still nothing and also this
Even following this video has me stuck on the session part.
And I just can not get this to work.
Remember Linux is case-sensative whereas Windows is case-insensative.
place you're MY_Staffcontroller inside application/core/MY_Controller.php file
Your MY_Controller.php file should look like this (minus all you're other functions, this is a minimal example)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
}
class MY_Staffcontroller extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function sayHello()
{
echo "Hello, I am a function within MY_Staffcontroller.php";
}
}
Example
This will be located in /application/controllers directory
Basically any protected and public functions located in either MY_Controller OR MY_Staffcontroller will be accessible from derived controllers that extend the extended controller. In this case it would be MY_Staffcontroller
class Public_Staff_Controller extends MY_Staffcontroller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->sayHello();
}
}
/* end of file /application/core/MY_Controller.php */

Extending HMVC modules in CodeIgniter

Let's say we have module called core_crud with something like this in the controller:
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Core_crud extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('mdl_core_crud');
}
public function index()
{
// code goes here
}
}
And now I want to extend this module with another module called shop_crud. How would the basic controller for this shop_crud module look like? I mean I want to inherit all the controller methods from core_crud and all the model stuff too.
Structure of the Modules
/modules
/core_crud
/controllers
/core_crud.php
/models
/views
/shop_curd
/controllers
/shop_crud.php
/models
/views
Code in core_crud.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Core_crud extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('mdl_core_crud');
}
public function index()
{
// code goes here
}
public function mymethod($param1 = '', $param2 = '')
{
return 'Hello, I am called with paramaters' . $param1 . ' and ' . $param2;
}
}
Code in shop_crud.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Shop_crud extends MX_Controller
{
public function __construct()
{
parent::__construct();
//$this->load->model('mdl_shop_curd');
}
public function testmethod()
{
// output directly
$this->load->controller('core_crud/mymethod', array('hello', 'world'));
// capture the output in variables
$myvar = $this->load->controller('core_crud/mymethod', array('hello', 'world'), TRUE);
}
}
So instead of extending the whole module/controller I prefer just to call the method which is required. It is simple and easy too.
Note If module name and controller name are different then you have to pass the path
module_name/controller_name/mymethod
EDIT to support EXTENDS
File structure
The code in core_crud.php.
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Core_crud extends MX_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('core_crud/mdl_core_crud');
}
public function index()
{
return 'index';
}
public function check_method($param1 = '')
{
return 'I am from controller core_crud. ' . $this->mdl_core_crud->hello_model() . ' Param is ' . $param1;
}
}
The code in mdl_core_crud.php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class mdl_core_crud extends CI_Model
{
public function hello_model()
{
return 'I am from model mdl_core_crud.';
}
}
The code in shop_crud.php.
if (!defined('BASEPATH'))
exit('No direct script access allowed');
include_once APPPATH . '/modules/core_crud/controllers/core_crud.php';
class Shop_crud extends Core_crud
{
public function __construct()
{
parent::__construct();
}
public function index()
{
echo parent::check_method('Working.');
}
}
Output :- I am from controller core_crud. I am from model
mdl_core_crud. Param is Working.
Hope this helps. Thanks!!
If you are loading the models in the parent class or in the construct then it should be inherited in shop_crud. are you not looking to do class Shop_crud extends Core_crud {? is parent::__construct() not retaining the construct for you?
Is this something you can handle with routing to the same controller rather than extending a controller (wanting to inherit both the controller and the model seems strange to me or something you could handle with a route and a private function in the class to handle the logic)?
"Controllers" this name defines it's functionality. The controller is used to control a particular section. So in MVC framework I think it's better to create individual controller for individual module. But you can reuse the model i.e. you can call one model's function in another model. For this
First load your model like $this->load->model("modelName"); in your controller
Then call the function like $this->modelname->functionName();
From what I can gather, you have to require the parent controller that you are extending. This isn't exactly ideal, but I'll look into a better way to do this later on. For now, I've created a simple function to do the inclusion.
function extend_module($module) {
$path = realpath(APPPATH) . '/modules/'. $module.'/controllers/'.ucfirst($module).'.php';
require_once($path);
}
Usage:
extend_module('some_module');
class othe_ module extends some_module {
NOTE: The function needs to be available outside of the CI object, so put it somewhere like your main index.php file.
Also note: As these variables are used to reference the local file system, do not dynamically assign them directly from user generated input. Doing so would cause multiple file system vulnerabilities.
Platform: CI3 + Bonfire 8 HMVC

Categories