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
}
}
Related
I need to access $data[] variables in whole site, in all views. I created TestLeangue controller and assign some values to $data but in all views say that variable is not defined
class TestLanguage extends MY_Controller
{
public function __construct() {
parent::__construct();
$this->lang->load("menu","english");
}
function index() {
$data['shipping'] = $this->lang->line('menu_shipping');
$this->load->view('templates/navigation', $data);
}
}
I echo and say that I have undefined variable.
<?= $shipping; ?>
Why don't you use that data like a global variable?
class MY_Controller extends ...
{
protected $data = [];
}
Then everytime when you extend your custom controller you will have access to the general data.
class MyCustomStackOverflowController extends MY_Controller
{
public function index()
{
$this->load->view('index', $this->data);
}
}
Also, you can use $this->data to load it with general information for current controller like:
class MyCustomStackOverflowController extends MY_Controller
{
public function __construct()
{
parent::__construct();
$this->data['title'] = 'Stackoverflow';
}
public function index()
{
...
}
}
Use Index of $data as a variable name ($shipping), to get the value that you have assigned as $data['shipping']
Controller
class TestLanguage extends MY_Controller
{
public function __construct() {
parent::__construct();
$this->lang->load("menu","english");
}
function index() {
$data['shipping'] = $this->lang->line('menu_shipping');
$this->load->view('templates/navigation', $data);
}
}
View :templates/navigation
<?php
print_r($shipping);
?>
Am trying to load view file in application/core/MY_Controller.php but its giving as follows.
Message: Undefined property: Edustaticlanding::$load
Filename: core/MY_Controller.php
Function is as follows in MY_Controller.php
function menu(){
$arrr = array();
return $arrdata['menu'] = $this->load->view('menu',$arrr,true);
}
And am calling this function in controller(Edustaticlanding.php) as follows.
function __construct(){
$this->menucontent = $this->menu();
print_r($this->menucontent); die;
}
Pls correct me.. where its going wrong.. thanks
On application/core/MY_Controller.php
Add public $data = array() like below
<?php
class MY_Controller extends CI_Controller
{
public $data = array();
public function __construct()
{
parent::__construct();
$this->data['menu'] = $this->menu();
}
public function menu(){
return $this->load->view('menu', NULL, TRUE);
}
}
On the controller then once add public $data = array(); you can access the menu on view
You have to use $this->data now on controller
<?php
class Example extends MY_Controller {
public $data = array();
public function index() {
$this->data['title'] = 'Welcome to Codeigniter';
$this->load->view('example', $this->data);
}
}
On the example view now you can echo
<?php echo $menu;?>
Add extends CI_Controller to your core controller like following codes:
class MY_Controller extends CI_Controller {
Please check below mentioned solution. You need to call parent constructor first. So it will load all basic configurations.
First extent CI_Controller class and call parent constructor like describe below
class MY_Controller extends CI_Controller {
public function __construct{
parent::__construct();
}
}
Please let me if it not works.
I am new to codeigniter.
I want to set a global variable for my helpers, models, and controllers.
the variable is getting from database..
Here is the example:
don't know where to put the variable
$this->load->model('test');
$data = $this->test->get_code();
$this->code = $data["code"]; //main global variable
my helper
function test() {
if($this->code=="test") {
}
}
my controller
function index() {
echo $this->code;
}
my model
function get_data() {
$query = $this->db->get_where('my_table', array('code' => $this->code));
return $query->row_array();
}
As you can see my scripts above, $this->code is almost used for my helper, my controller, and my model.
Where should i put the variable so that i can access the variable by using $this->code only to all?
You can crate MY_Controller in extends MX_Controller in application/core folder and extends MY_Controller in Application/module create you own controller.EX see below
class MY_Controller extends MX_Controller {
public $code = ""; // Global variable
public function __construct() {
parent::__construct();
$this->load->model('test');
$data = $this->test->get_code();
$this->code = $data["code"]; //main global variable
}
}
class Admin extends MY_Controller {
function index() {
echo $this->code;
}
}
Try put data to session:
$this->load->model('test');
$data = $this->test->get_code();
$this->load->library('session');
$this->session->set_userdata('name' => $data );
and
function test() {
$this->load->library('session');
$data = $this->session->userdata('name');
if($data=="test") {
}
}
function index() {
$this->load->library('session');
$data = $this->session->userdata('name');
}
You may use session without load in every function,
add in config/autoload.php
$autoload['libraries'] = array('session');
So here is my controller:
class Search extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('search_model');
$this->search_model->search_result = $_POST;
}
public function index()
{
$data['results'] = $this->search_model->get_results();
$this->load->view('search_results', $data);
}
And here is my model:
class Search_model extends CI_Model {
protected $search_query;
function __construct($search_query)
{
parent::__construct();
$this->load->database();
$this->search_query = $search_query;
}
But this doesn't seem to work. What I want to do is pass the posted form ($_POST) to my model, then do stuff with it. But it seems messy to pass $_POST to each method of my model. My plan is to extract the variables sent with $_POST and construct these as properties such as $website_url, $text_query etc..., then call these in methods with $this->website_url;
I'm relatively new to CodeIgniter so just getting to grips with the basics
for your special purpose you can try this code
Controller:
class Search extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('search_model');
$this->init();
}
private function init()
{
$this->search_model->init( $this->input->post() );
}
public function index()
{
$data['results'] = $this->search_model->get_results();
$this->load->view('search_results', $data);
}
model:
class Search_model extends CI_Model {
protected $search_query;
function __construct()
{
parent::__construct();
$this->load->database();
}
public function init( $search_query )
{
$this->search_query = $search_query;
}
you have protected $search_query; which you can't access it from your controller.
You either have to change it to public or create getter and setter for it. or just getter depending on your domain/business logic.
And it should have been obvious as you should get an error saying
Fatal error: Cannot access protected property in file some/path/to/file!
Don't put the 'search query' in your model constructor.
Controller:
class Search extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('search_model');
}
public function index()
{
if ($this->input->server('REQUEST_METHOD') == 'POST')
{
// you should probably validate/clean the post data instead
// of sending it straight to the model
$results = $this->search_model->get_where($_POST);
}
else
{
// if it's not a post, you probably need something...
// either here, or somewhere in your view to handle empty data
$results = array();
}
$data['results'] = $results
$this->load->view('search_results', $data);
}
Your Model:
class Search_model extends CI_Model {
function __construct()
{
parent::__construct();
$this->load->database(); // <--- you may want to autoload 'database' library
}
function get_where($where)
{
$this->db->where($where);
// add select, order, joins, etc here
return $this->db->get('mytable'); // whatever your table name is
}
I have my controller
class Page extends CI_Controller {
public function id() {
$this->load->model('content');
$page = $this->uri->segment(3, 0);
if($page == 0)
$page = $this->content->get_default_page($page);
$data['navigation'] = $this->content->getNav();
$data['pagename'] = $this->content->get_pagename($page);
$data['content'] = $this->content->get_content($page);
$this->load->view('main', $data);
}
}
Now I'll try to explain.
Im getting navigation, and navigation text from mysql (id, navName, navText).
then im returning those elements in views/main_view.php in url like: http://abc.com/page/id/1 etc...
Now i need to create other controller like mySuperDuperModule which have some functions not just text.
The problem is that if i create new controller like Gallery(), i need to copy all the stuff from Page() controller to make website show the same.
Is there any way not to do that ?
You can create base controller under /application/core/MY_Controller.php
Here MY_ is the value specified for $config['subclass_prefix'] in /application/config/config.php
class MY_Controller extends CI_Controller
{
protected $data;
public function __construct()
{
parent::__construct();
$this->data = array();
}
public function loadPage($page)
{
$this->load->model('content');
if($page == 0)
$page = $this->content->get_default_page(); // I hope this function will give default page from database table. (A Change from your code)
$this->data['navigation'] = $this->content->getNav();
$this->data['pagename'] = $this->content->get_pagename($page);
$this->data['content'] = $this->content->get_content($page);
}
}
Your modified Page Class in /application/controllers/page.php
class Page extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function id()
{
$page = $this->uri->segment(3, 0);
parent::loadPage($page);
$this->load->view('main', $this->data);
}
}
and your new gallery controller can be in /application/controllers/gallery.php
class Gallery extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function id()
{
$page = 10; // Assuming gallery page index is 10 in database table OR you can change any logic here.
parent::loadPage($page);
$this->load->view('main', $this->data);
}
}
you can create as many controllers as you want by extending MY_Controller.
You can pass these two lines to any controller
and they will display the same template.
$data['navigation'] = $this->content->getNav();
$data['pagename'] = $this->content->get_pagename($page);
If you still dont understand try to use this library
https://github.com/philsturgeon/codeigniter-template