CI load properties by default - php

I use Codeigniter Framework, and I set my program to load header and footer by default when i load the view method. In the header file I have properties like: site name, description etc.. Those proprieties fetch from the DB. Now the problem is that I need to set them every time I call the view method.
How can I set them by default correctly?

As someone said above, you should extend the Controller. I do something very simular to this, and this is my code, which is found on "MY_Controller.php" within the ./application/core directory.
public function show_view($view, $data = array())
{
// Database connection here
// add anything to the $data array
$this->load->view('header', $data);
$this->load->view($view, $data);
$this->load->view('footer', $data);
}
Now, instead of loading a view from the controller like this;
$this->load->view('view', $data);
I do this;
$this->show_view('view', $data);
FYI. I call the function "show_view" to avoid name conflicts.

Related

Passing data to view from controller using templates

Controller:
public function home(){
$data['page_title'] = 'Home';
$data['navbar_content'] = $this->model->get($var);
$data['page_content'] = $this->model->get($var1);
$this->load->view('template/header');
$this->load->view('template/navbar');
$this->load->view('pages/home');
$this->load->view('template/footer');
}
Using the sample code above, Since codeigniter only allows the passing of variables/data through an array or object from controller to the view then what is the 'right' way to pass my data?
I could simply pass the data when loading the header and things will still work out fine.
$this->load->view('template/header', $data);
$this->load->view('template/navbar');
$this->load->view('pages/home');
$this->load->view('template/footer');
But that doesn't seem 'right' to me, i guess i wanted things to be clear in my code for example:
$this->load->view('template/header', $title);
$this->load->view('template/navbar', $navbar_content);
$this->load->view('pages/home', $page_content);
$this->load->view('template/footer');
The above is clear cut and there is a distinction to the data that are being passed.
But since i can't do that, is there a 'correct' way of doing this? or am i thinking too much on it?
I neglected to mention, I could simply make 2 arrays to pass to the view. I thought this is wrong in the first place since i'm going out of my way to make 2 arrays to pass just so i can pass a single string variable to my header page.
Ofcourse when i pass more data to my navbar then it would make sense to me.
This is the correct way according to the documentation:
https://codeigniter.com/userguide3/general/views.html
Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading method.
You should also look on:
Codeigniter: Best way to structure partial views
You should structure you views hierarchically.
$this->load->view('template/header', ["title" => $title]);
$this->load->view('template/navbar', ["navbar_content" => $navbar_content]);
$this->load->view('pages/home', ["page_content" => $page_content]);
$this->load->view('template/footer');
CodeIgniter passes data from controllers to views.
You need to pass $data to views.
You will pass an array with its keys be strings or even arrays (multi-dimensional arrays also).
So, you need to pass it to every view you are using $data.
$this->load->view('template/header', $data);
$this->load->view('template/navbar', $data);
$this->load->view('pages/home', $data);
$this->load->view('template/footer', $data);
And in your view, you can access $data with keys.
For example:
if your data is
$data['header_banner'] = 'Header Banner';
In your view, you can access this value by:
echo $header_banner;
Observe that to the key in $data, you just need to add a $ and you can access that variable.
Its something line PHP's in built function extract()

Get data from model to every view using OOP and DRY

I have a piece of data I want passed to every view. I am using CodeIgniter 3 and have PHP 7 available to me. The current way I do it is using something like this in every function.
$data['foobar'] = $this->general_model->foobar();
// More code
$this->load->view('homepage', $data);
I'd prefer not to have to call $data['foobar'] = $this->general_model->foobar(); on every single function.
I've tried many approaches to fix this without resorting to anything that makes the code too goofy. I've tried constructors, autoload, and hooks. The problem in each case boils down to the fact that $data is local to each function. The best I've gotten is usually something like this.
$data['foobar'] = $this->foobar;
// More code
$this->load->view('homepage', $data);
This is slightly nicer, but it still results in me placing this line in every function.
I'd like my functions to in someway inherit $data with the index foobar already set. I'd prefer to avoid a solution that requires every function receiving $data as a parameter. How can I accomplish this?
Option 1:
Not sure if you have tried this but you could set $data as a property of your class
protected $data = [];
Then in your constructor set it.
$this->data['foobar'] = $this->general_model->foobar();
This would mean your $data becomes accessible to all your methods in your controller and you would need to refer to them as $this->data['data_name'] and use it in a view like
$this->load->view('homepage', $this->data);
Option 2:
A second way is to create a method like render() which is common to all your methods that load views and replaces your existing view calls.
So you would have something like...
public function one_of_my_methods(){
$data['content'] = 'This is content 1';
$this->render('test_view',$data); // Call the new view handler
}
// All methods using views now call this to load the final view
public function render($view,$data){
$data['foobar'] = 'I am common'; // DRY
$this->load->view($view, $data);
}

Load view from helper Codeigniter?

Can i load view from helper in codeigniter? I have been looking for a present, but it seems no one has discussed it.
Yes, you can. Create your helper, say views_helper.php:
if(!function_exists('view_loader')){
function view_loader($view, $vars=array(), $output = false){
$CI = &get_instance();
return $CI->load->view($view, $vars, $output);
}
}
$view is the view file name (as you would normally use), and $vars an array of variables you want to pass (as you would normally do), pass a true as optional third parameter to have it returned (as it would normally happen) as content instead of it just being loaded;
Just load your helper (or autoload it):
$this->load->helper('views');
$data = array('test' => 'test');
view_loader('myview', $data)

how do we pass data from controller to view in zend?

I am starting in zend framework 1.11. How do we pass different $data value in view from controller to view like in codeigniter we pass like this.
$data['pass_one_thing'] = $this->model1->pass_all_mangoes();
$data['pass_another_thing'] = $this->model2->pass_all_oranges();
$this->load->view('viewfile', $data);
then in views we get values of $pass_one_thing and $pass_another_thing with foreach loops in same view file.
how do i pass from different model function in a same view ?
How do we get such thing in zend ? I am new to zend and bit confused.
You set it in your controller as:
$this->view->myVar = "something";
And then access it from the view:
echo $this->myVar;
Or using assign like Wesley said.
That can be done pretty much the same:
$this->view->data = $data;
Or use the assign function:
$this->view->assign('data', $data);
edit:
How do I pass from different model function in a same view
Not exactly sure but taking your exact example:
$this->view->data['pass_one_thing'] = $this->model1->pass_all_mangoes();
$this->view->data['pass_another_thing'] = $this->model2->pass_all_oranges();
$this->load->view('viewfile', $data);
Then in your view you would access these trough:
$this->data['pass_one_thing']
$this->data['pass_another_thing']

Query caching in CodeIgniter

I want to cache a query in CodeIgniter. What I did for my test is make a controller, that I named show.php:
class Show extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('rejaal_show');
}
public function _remap($method = '',$param = array())
{
$method = intval($method);
$this->output->cache(5);
var_dump ($this->rejaal_show->temp($method));
}
}
And a model that I named rejaal_show.php:
public function temp($id)
{
$this->db->cache_on();
$this->db->where('id',$id);
$query = $this->db->get('system_store_table');
return $query->result();
}
When I call http://localhost/rejaal/show/1 for the first time, it will show a result, but when I call it for the second time, it does not show anything.
I should delete the query cache file to show it again? How should I solve this problem?
With special thanks for your attention.
Can you confirm that you have set $db['default']['cachedir'] to the path of a writable folder in application/config/database.php and that when the query is first run it creates a cache file in there?
The only other reason I can think of for it failing is by your use of the _remap override. I have not used db caching using _remap, but know that CodeIgniter creates a folder called controller+action in your cache folder, and might not be handled very well if using remap? Someone correct me if I am wrong about this.
In the CodeIgniter User Guide page for Web Page Caching, it says:
Because of the way CodeIgniter stores content for output, caching will only work if you are generating display for your controller with a view.
Do your var_dump inside a view.

Categories