How to load same content over all page? - php

Instead of calling same data inside each functions in controllers i want to load that data globally by calling once.
i did
MY_Controller.php
<?php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url','file'));
$this->load->model('common/common_model');
$data['header_menus'] = $this->common_model->categoryMenus();
}
}
This file is inside core folder and in controller folder there is a controller and i did
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends MY_Controller {
while loading function inside home controller how do i load $data['header_menus'] in views do i need to do saomething with that variable in function again?

Create one view in View folder and load that view in controller, make changes in your controller:
For example, your view name is display_view.php in view folder:
MY_Controller.php
<?php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url','file'));
$this->load->model('common/common_model');
$data['header_menus'] = $this->common_model->categoryMenus();
$this->load->view('tour/view',$data);
}
}
display_view.php
<?php
if(!empty($header_menus)) {
extract($header_menus);
}
print_r($header_menus); // you can get all the info here
?>
Load View in Codeigniter

Check about inheritance.
$data['header_menus'] = $this->common_model->categoryMenus();
has to be
$this->data['header_menus'] = $this->common_model->categoryMenus();
In extending controller you need to call it with
$this->data['header_menus'];
Use $this->data array for other variables too and call the view with something like:
$this->load->view('some_view_file', $this->data);
Read in docs here, here, here, here and here.

Related

Class 'BaseController' not found

I am using PHP MVC CI. BaseController and LoginController are both inside Controller Folder. When I run the Login Controller. It says..
Class 'BaseController' not found
Login Controller
<?php
if ( ! defined('BASEPATH')) die('No direct script access allowed');
class loginController extends BaseController {
function __construct() {
parent::__construct();
}
public function login() {
$this->load->View("template/header");
$this->load->View("login_view");
}
}
?>
Base Controller
<?php
class BaseController extends CI_Controller {
function __construct()
{
session_start();
$this->load->library('session');
parent::__construct();
}
}
?>
Any idea why this is happening ?
If you need a parent controller than should be in
Path - application/core/MY_Controller.php
http://www.codeigniter.com/user_guide/general/core_classes.html
<?php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
}
If you also autoload sessions then you will not need to use session_start.
http://www.codeigniter.com/user_guide/libraries/sessions.html
Filename: Welcome.php must be first letter uppercase in CI3
<?php
class Welcome extends MY_Controller {
}
CI 3 is case sensitive
Both user guides now here. CI2 & CI3 http://www.codeigniter.com/docs
You just need to change the file path , as follows:
application > controllers > Login.php
<?php
if ( ! defined('BASEPATH')) die('No direct script access allowed');
class loginController extends BaseController {
function __construct() {
parent::__construct();
}
public function login() {
$this->load->View("template/header");
$this->load->View("login_view");
}
}
?>
And,
application > core > Base_controller.php
<?php
class BaseController extends CI_Controller {
function __construct()
{
session_start();
$this->load->library('session');
parent::__construct();
}
}
?>
Codeigniter wont load it from the same folder.
You either put both classes in the same file, not desirable or put the base controller in application/core.
You will need to also set your prefix for you extended controller, for example BASE_Controller.
application/config/config.php
$config['subclass_prefix'] = 'BASE_';
Codeigniter has good docs, and what you are after can be found here

calling x controller function inside a y controller function codeigniter

I'm new to codeigniter and I have two controllers: utility.phpand welcome.php.
In utility.php, I have functions:
function getdata() {
//code here
}
function logdata() {
//code here
}
Inside welcome.php, I have this function:
function showpage() {
//some code here
//call functions here
}
What I want to do is inside my welcome.php, I want to call the functions from utility.php. How do I do this? Thanks.
Refrence from here
To extend controller please either follow this tutorial or see some code below.
differences between private/public/protected
make a file in folder /application/core/ named MY_Controller.php
Within that file have some code like
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
protected $data = Array(); //protected variables goes here its declaration
function __construct() {
parent::__construct();
$this->output->enable_profiler(FALSE); // I keep this here so I dont have to manualy edit each controller to see profiler or not
$this->load->model('some_model'); //this can be also done in autoload...
//load helpers and everything here like form_helper etc
}
protected function protectedOne() {
}
public function publicOne() {
}
private function _privateOne() {
}
protected function render($view_file) {
$this->load->view('header_view');
if ($this->_is_admin()) $this->load->view('admin_menu_view');
$this->load->view($view_file . '_view', $this->data); //note all my view files are named <name>_view.php
$this->load->view('footer_view');
}
private function _isAdmin() {
return TRUE;
}
}
and now in any of yours existing controllers just edit 1st or 2nd line where
class <controller_name> extends MY_Controller {
and you are done
also note that all your variables that are meant to be used in view are in this variable (array) $this->data
example of some controller that is extended by MY_Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class About extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->data['today'] = date('Y-m-d'); //in view it will be $today;
$this->render('page/about_us'); //calling common function declared in MY_Controller
}
}
You don't is the short answer.
The point of MVC is to have your code well organized.
What you can do though is create a library in the libraries folder where you put methods you need in more than one controller.
For example you can make a mylog library, where you can put all your log related stuff. In any controller you will then call:
$this->load->library('mylog');
$this->mylog->logdata();
Besides functions that deal with data models should reside in models. You can call any model from any controller in CI
That is out of concept, if you want to call code in different controllers do following:
create helper and call function whenever (even in view) you want.
extend CI_Controller so you can use one or more functions in any controller that is extended.
Lets start with helper:
create file in folder application/helpers/summation_helper.php
use following sample of code
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function sum($a, $b) {
//$CI =& get_instance();
// if you want to use $this variable you need to get instance of it so now instead of using $this->load... you may use $CI->load
$return = $a + $b;
return number_format($return, 3);
}
If you are going to use your helper in many controllers/views please load it in autoload, otherwise just load it manualy $this->load->helper('summation');
Extending Controller_CI: this is way better approach if you are using database. Please follow this tutorial, which explains it all.
*I have crafted an answer just before site went down posting this from cellphone.

How to call one controller function in another controller in codeigniter

I have one controller named home.php in which a function named podetails is there. I want to call this function in another controller user.php.
Is it possible to do so? I have read about HMVC in CI, but I want to know is it possible to do without using hmvc?
To extend controller please either follow this tutorial or see some code below.
differences between private/public/protected
make a file in folder /application/core/ named MY_Controller.php
Within that file have some code like
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
protected $data = Array(); //protected variables goes here its declaration
function __construct() {
parent::__construct();
$this->output->enable_profiler(FALSE); // I keep this here so I dont have to manualy edit each controller to see profiler or not
$this->load->model('some_model'); //this can be also done in autoload...
//load helpers and everything here like form_helper etc
}
protected function protectedOne() {
}
public function publicOne() {
}
private function _privateOne() {
}
protected function render($view_file) {
$this->load->view('header_view');
if ($this->_is_admin()) $this->load->view('admin_menu_view');
$this->load->view($view_file . '_view', $this->data); //note all my view files are named <name>_view.php
$this->load->view('footer_view');
}
private function _isAdmin() {
return TRUE;
}
}
and now in any of yours existing controllers just edit 1st or 2nd line where
class <controller_name> extends MY_Controller {
and you are done
also note that all your variables that are meant to be used in view are in this variable (array) $this->data
example of some controller that is extended by MY_Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class About extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->data['today'] = date('Y-m-d'); //in view it will be $today;
$this->render('page/about_us'); //calling common function declared in MY_Controller
}
}
write the podetails() as a function within a helper file.
then load that helper in both of the controllers.
in the controller you just call podetails()
Suppose:
--controller 1--
function podetails()
{
podetails(); // will call function in helper ;
}
--controller 2--
function podetails()
{
podetails(); // will call function in helper ;
}

Codeigniter: Error in loading HMVC sub module model

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

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