I have two layout templates in my project and always loading first template. How to load second template also.
In main Usercontroller how to differentiate them. They are,
Learncontroller.php
class Learncontroller extends Usercontroller{
public function __construct(){
parent::__construct();
$this->load->model("Usermodel","",true);
}
public function index(){
$id=$this->session->userdata('cp_userid');
$menuActive= "learning";
$data['menuActive'] = $menuActive;
$userdetails=$this->Usermodel->getuserdetails($id);
$data['userdetails']=$userdetails;
$result=$this->Usermodel->currentlearningcourses($id);
$data['details']=$result;
$data['content']=$this->load->view("user/currentlearningcourses",$data,true);
$data['title']='My Courses';
//$this->load->view("user/layout",$data);
$headerContent = $this->load->view("user/layout",$data,true);
$this->render($headerContent);
}
}
Unitcontroller.php
class Unitcontroller extends Usercontroller{
public function __construct(){
parent::__construct();
$this->load->model("Usermodel","",true);
}
public function courselearn($id){
$r=$this->Usermodel->getsectiontopic($id);
$data['topicId']=$id;
$data['topicQ']=$r;
$data['video']=$this->input->post("id");
$data['id']=$id;
$data['content']=$this->load->view("user/unit_content",$data, true);
$courselearnheaderContent = $this->load->view("user/courselearn_layout",$data,true);
$this->render($courselearnheaderContent);
}
}
Learncontroller and Unitcontroller has separate layouts but always loading protected $layout = 'user/layout';. How to load this 'user/courselearn_layout'; when control comes from Unitcontroller.
Usercontroller.php
protected $layout = 'user/layout';
protected $courselearn_layout = 'user/courselearn_layout';
protected function render($headerContent) {
$view_data = array( 'headerContent' => $headerContent);
$this->load->view($this->layout);
}
Always loading first page. How to make load the second page also please help me.
Related
How to make a global variable that always changing?
I want to create something like protected $global_data; in controller Global_data.php that can be called by a lot of controller, and return variable $global_data value to whenever controller that has $this->load->library('../controllers/Global_data').
But when I tried to call it, it gives me this error Unable to locate the specified class: Session.php, so I think CodeIgniter 3.1.8 not allowed me to do this.
So how to achieve what I'm looking for? do I need to put it on model instead, library file or is there another way to do it?
Thank you.
Here is Global_data.php content
protected $global_data;
public function __construct()
{
parent::__construct();
$this->global_data = array(
'can_be_anything' => 'can_be_anything',
'can_be_anything' => 'can_be_anything',
'can_be_anything' => 'can_be_anything',
'can_be_anything' => 'can_be_anything',
);
}
Can_be_anything_controller.php content
class Can_be_anything_controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('../controllers/Global_data');
}
public function index()
{
$data = $this->global_data;
$data['page_title'] = 'Dashboard';
$data['page_directory'] = 'pages/dashboard';
$this->load->view('template', $data);
}
}
You may create a library for that in libraries directory
Global_data.php file
class Global_data{
public $global_data;
protected $CI;
public function __construct() {
$this->CI = & get_instance();
}
public function common_data()
{
$this->global_data= array(
'can_be_anything' => 'can_be_anything',
'can_be_anything' => 'can_be_anything',
'can_be_anything' => 'can_be_anything',
'can_be_anything' => 'can_be_anything',
);
return $this->global_data;
}
public function any_method(){
$query = $this->CI->db->get('table_name');
}
}
Now you can load it in any controller like
$this->load->library('Global_data')
Then use data
$data = $this->Global_data->common_data();
Also you may use HMVC model to use any method in any controller
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
Make a MY_Controller in application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
public $global_data;
public function __construct() {
parent::__construct();
$this->global_data = 'whateveryouwant';
}
public function somemethod() {
return '123';
}
}
then any other controllers in your application/controllers that need to access global_data should extend it like so:
class Somecontroller extends MY_Controller {
public function index() {
echo $this->global_data;
echo $this->somemethod(); // works with methods too
}
}
If you need to run more complex code just put everything into a library or model and autoload it. All public methods and properties are globally available. HMVC seems overkill for what you want.
This kind of data can also be handled very nicely by config files.
/application/config/global_data.php
<php
$config['foo'] = "some foo";
$config['bar'] = 42;
$config['baz'] = array('one', 'two', 'three');
In a controller load the config file with
$this->config->load('global_data');
The access the items using
echo $this->config->item('foo');
echo $this->config->item('bar') * 2; //outputs 84
$data = $this->config->item('baz');
Config documentation
contrller:News.php
This is my controller News
<?php class News extends CI_Controller {
public function __construct()
{
}
public function getShowIN_News()
{
return $result;
} } ?>
contrller:Category.php
This is my controller Category
<?php class Category extends CI_Controller {
public function __construct()
{
}
public function category()
{
require('news.php');
$test = new News();
$data["headlines"] = $test->getShowIN_News();
} }?>
By using an empty constructor, you're making it so that CI_Controller::__construct() isn't called, and that's where everything in the framework is initialized.
I know you've put it there to hack it so you can call one controller from another, but it is very intentionally made that way, exactly so you don't do this.
I am creating a master controller so that every other controllers on my app extend the master (MY_Controller).
My problem is how to make loading a model more abstract, let me show you the code to better explain.
class MY_Controller extends CI_Controller
{
protected $model;
function __construct()
{
parent::__construct();
}
function get($order_by)
{
$this->load->model($this->model);
$query = $this->$this->model->get($order_by);
return $query;
}
}
I declare a protected variable $model in the master controller so that on the extending controller i can asign it a value:
class Home extends MY_Controller {
public function __construct() {
parent::__construct();
$this->model = "home_model";
}
public function index()
{
$test = $this->get('id');
}
}
The problem is that on MY_Controller i cant load the model
$this->$this->model->get();
i get the following error: Message: Object of class Home could not be converted to string
any help will be appreciated , thank you !
Why do not save the value you assign to protected global $model variable in a variable inside the function as such:
class MY_Controller extends CI_Controller{
protected $model;
function __construct(){
parent::__construct();
}
function get($order_by){
$_model = $this->model // I added this line.
$this->load->model($_model); // I modified this line.
$query = $this->$_model->get($order_by); // I modified this line.
return $query;
}
}
The $this->$this->model->get() is your problem. If you want to use an object property ($this->model) inside a chain like this, you need to wrap it in braces: $this->{$this->model}->get().
I want every controller to have a method _render_page, which loads the master template and passes the data object.
My home controller class looks like this:
class Home extends MY_Controller {
function __construct() {
parent::__construct();
}
public function index() {
$data['title'] = "Site title";
$data['the_view'] = 'welcome_message';
$this->_render_page($this->layout, $data);
//$this->load->view($this->layout, $data); //This works ok..
}
}
MY_controller class:
class MY_Controller extends CI_Controller {
public $layout;
public $viewdata;
public function __construct() {
parent::__construct();
$this->layout = 'layout/master_template';
$this->viewdata = null;
}
public function _render_page($view, $data=null, $render=false) {
$this->viewdata = $data;
$this->viewdata['the_view'] = $view;
if($this->ion_auth->logged_in()) {
$user_obj = $this->ion_auth->user()->row();
$usr_data['username'] = $user_obj->username;
$user_obj = null;
$this->viewdata['usr_data'] = $usr_data;
}
$this->load->view($this->layout, $this->viewdata); //The code crashes here
}
}
When I browse to home controller I get nothing, just white screen no errors...
Found a solution: I'm calling _render_page in a wrong way.
Instead of this:
$this->_render_page($this->layout, $data);
I should call like this:
$this->_render_page('welcome_message', $data);
Of course, that's what this function is about - to load a master page and pass the view name as member of $data, so the master page will know which view to load.
See, you need to understand the flow going,
when you call your class home , it extends MY_Controller, CI will look for MY_Controller, the constructor of your MY_controller gets executed after which CI starts executing the constructor of your home controller then your default method of home controller,
so in order to make it work you need to call _render_page()-
change your MY_Controller like -
class MY_Controller extends CI_Controller {
public $layout;
public $viewdata;
public function __construct() {
parent::__construct();
$this->layout = 'layout/master_template';
$this->viewdata = null;
$this->_render_page($this->layout, $data=null, $render=false); // call your method
}
public function _render_page($view, $data=null, $render=false) {
$this->viewdata = $data;
$this->viewdata['the_view'] = $view;
if($this->ion_auth->logged_in()) {
$user_obj = $this->ion_auth->user()->row();
$usr_data['username'] = $user_obj->username;
$user_obj = null;
$this->viewdata['usr_data'] = $usr_data;
}
$this->load->view($this->layout, $this->viewdata); //The code crashes here
}
}
file:MY_Exceptions.php,
class MY_Exceptions extends CI_Exceptions {
public function __construct(){
parent::__construct();
}
public function show_404($page = ''){
//from helper, helper's loading..correctly. so are libs
$data=common_site_data();
$CI->load->view('site/templates/header',$data);
$CI->load->view('site/home/404',$data);
$CI->load->view('site/templates/footer',$data);
}
}
But no views are loading..! Not even the errors/error_404.php..!
What's the heck? Can anyone help this out?