I am wondering if Zend view variables are available in my view helper class without passing them in directly as parameters
thanks
Luca
As all view helpers have reference to the view in their $view property, the answer is yes.
What you won't know from the helper side is which properties are available. It would be better to pass any required properties to the helper at call or construction time.
well you can access $view form inside the view helper , i will give an example :
in the example below you can set and get view vars
<?php
class App_View_Helper_Job extends Zend_View_Helper_Abstract {
public function setView(Zend_View_Interface $view) {
$this->view = $view;
}
public function job() {
$this->view->var1 = "testing var1 ";
$this->view->var2 = $this->view->var1;
}
}
You should know one thing:
view helper's view instance is the one set on helper instantiation It is not updated on view cloning. So you can't say for sure which one you're using if called from partial for example.
I found that when I set the view instance in registry and get it from helper the view variables stays. Here is a code snippet I used in my social engine project
$view = Zend_Registry::get('Zend_View');
/*
* Check data available and set it to local variable
*/
if(isset($view->localeTranslations[$key]))
{
$translate = $view->localeTranslations[$key];
}
Related
I have some data array which I need for all method's in a controller.
$data['project_ongoing_res_limit']=$this->admin_model->show_project_ongoing_residential_limit();
$data['project_ongoing_com_limit']=$this->admin_model->show_project_ongoing_commercial_limit();
$data['project_upcoming_res_limit']=$this->admin_model->show_project_upcoming_residential_limit();
$data['project_upcoming_com_limit']=$this->admin_model->show_project_upcoming_commercial_limit();
$data['project_completed_res_limit']=$this->admin_model->show_project_completed_residential_limit();
$data['project_completed_com_limit']=$this->admin_model->show_project_completed_commercial_limit();
Problem is I cant DRY this. so I have paste this $data array in each method.
I have a view page for this. so when I load this view , I have to
load above $data array each time/method. this is disgusting when controller
methods are too much.
I want 1 piece of this code like constructor. How can I do this.
you can use traits for this.
Define your methods in a trait, and then use the trait in the controllers.
You can make one helper class in which make a function and put your above code in it but make sure you can't access model using $this so, you need to create CI instance and than access it. after that in your controller in construct method you just need to call this function but don't forget to load the helper class and store it in a variable and pass it along with view.
Just create private data variable in your controller class. Than set your data in constructor. Now you can access your data in any method you want.
class Pages extends CI_Controller {
// ...
private $data;
// ...
public function __construct() {
parent::_construct();
$this->data = array();
$this->data['project_ongoing_res_limit']=$this->admin_model->show_project_ongoing_residential_limit();
$this->data['project_ongoing_com_limit']=$this->admin_model->show_project_ongoing_commercial_limit();
$this->data['project_upcoming_res_limit']=$this->admin_model->show_project_upcoming_residential_limit();
$this->data['project_upcoming_com_limit']=$this->admin_model->show_project_upcoming_commercial_limit();
$this->data['project_completed_res_limit']=$this->admin_model->show_project_completed_residential_limit();
$this->data['project_completed_com_limit']=$this->admin_model->show_project_completed_commercial_limit();
}
// ...
}
I simply do not mean how to define a global variable/constant in CodeIgniter.
Let me explain:
I have made a theme engine which is select-able from the current logged in user's control panel. This engine is not very complicated, but a simple folder. anyway, what I do across the application, is that I write one line of code to get the current theme selected by the user. I use a one line of code to get the name, and then store it in a variable:
$theme_name = $this->theme->get_theme_with_slash(false);
And then, I user $theme_name like this to get the proper view:
$this->load->view($theme_name.'result', $data);
And in all of my controllers that loads view I should repeat this process. What I need, is to call the function which gets the theme_name and store in the variable and then use the variable/session/function across the application. My approach currently is the helper's function which is a little less convenient compared to session/variable.
I got this from the manual and this what I have at the top of my config/config.php file: (i have a custom config set to paypal testing)
// HOW TO USE - For example if there's $config['foo'] = 'bar';
// in the config
// using $this- >config->item('foo') will be 'bar'.
// example for my paypal testing:
$config['paypaltest']=0;
http://ellislab.com/codeigniter%20/user-guide/libraries/config.html
and how to access in a controller:
$paypaltest = $this->config->item('paypaltest');
Create A core controller, since your process requires logical operations then you need a method for that.
application/core/MY_Controller.php
class MY_Controller Extends CI_Controller
{
protected $default_theme = 'theme';
public function __construct()
{
parent::__construct();
}
public function get_theme()
{
//your code for selecting the current theme selected from
//the database
$theme_from_db = '';
return $theme_from_db == NULL ? $this->default_theme : $theme_from_db;
}
}
Your Controller must extend MY_Controller
application/controller/view.php
class view extends MY_Controller
{
public function index()
{
$this->load->view($this->get_theme().'result', $data);
}
}
in code igniter global constants can be defined in
config->constants.php
even you no need to load it,it automatically autoloaded by CI automatically.
In Codeigniter all constant is defined inside application/config/constant.php.
like: define("CONSTANTNAME","value");
Constant degined here is accessible throughout all pages, ie; controllers, models and views
I have this question: how to load function in codeIgniter in every page and get data or variables from it to show in view?
I have many controllers and each one has many function. Each function load master view as a template and load its own view. Master view has dynamic data I push it from database.
So I need in master view to show some data from database automatically. How to do it?
You can use this. change it according to your
function index()
{
$data['list'] = $this->some_model->show_data();
$data1['content'] = $this->load->view('some_view',$data,true);
$this->load->view('template',$data1);
}
content is in your main template where u will pass your data to show in template
I am not sure what exactly you ask but if you want a function to be loaded automatically to any controller
then create a Mycontroller.php in application/core
class Mycontroller extends CI_Controller
{
function __construct()
{
parent::__construct();
// here goes your function
}
}
and then extend every controller you have to Mycontroller instead of CI_Controller
if you want to use your function inside views. use CI Helpers
Codeigniter Helpers
create your helper function
function get_data() {
// and use CI instance
$CI = & get_instance();
// now you can call your models, libraries. etc. in the helper
$data = $CI->your_model->your_model_funcion();
return $data;
}
and you can call your helper function in controllers, views, models. etc...
get_data();
I'm little bit new to Zend, I want to use a controller action through out the entire application automatically, I don't have a clear idea to how to use it, thought about init() method, action helpers, etc.
Then instead of simply creating controller action create controllerAction Helper . Here you can find more about it
http://framework.zend.com/manual/en/zend.controller.actionhelpers.html
My_Helper_Magic extends Zend_Controller_Action_Helper_Abstract
{
public function preDispach()
{
//code inside here will run for entire application automatically
}
}
In your bootstrap do
Zend_Controller_Action_HelperBroker::addHelper(new My_Helper_Magic());
In Response to comment .
It depends upon your 'code fragment' , If your code fragment does not required to know nothing about module , controller, action , base url then you can use Bootstrap init function
like
public function _initAlways()
{
//see how this function name is prefixed with _init hence it will be called by ZF //everytime. You can put your code fragment here
//If your code fragment depends upon some stuff like baseurl then do action controller
// registration here instead
Zend_Controller_Action_HelperBroker::addHelper(new My_Helper_Magic());
}
In Response To comment
You can save any instance of your object inside Zend_Registy and retrieve it whereever you like
Inside Bootstrap.php
public function _initSetup()
{
$object = new My_Custom_Object();
Zend_Registry::set('my_custom_object',$object);
}
Later in your view or controller do
$myObject = Zend_Registry::get('my_custom_object'); //to access it
PROBLEM: I can't pass my variables to my custom layout from action helper's init method.
I have this action helper "My_Action_Helper_Initializer":
class My_Action_Helper_Initializer extends Zend_Controller_Action_Helper_Abstract
{
public function init()
{
$controller=$this->getActionController();
//variable passed to controller's view
$controller->view->flop="FLOOP!!";
//variable passed to controller
$controller->boom="BOOM!!";
}
}
In my controller "IndexController":
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
//print the variable passed from action helper
echo $this->boom;
}
}
then in my "layout.phtml":
//print variable passed from action helper
echo $this->flop;
So the "boom" variable echoed out by controller action is shown correctly.
The "flop" variable (passed to my layout) is not shown.
QUESTION: Why the variable passed to controller action is correctly output while the other one passed to the layout view is not?
Thanks
Luca
When your helper's init() called, ViewRenderer's init() wasn't yet. This is because of order in helpers stack.
If you enable strict standards error reporting, you should see something like this in your helper "Creating default object from empty value in ..."
You should consider moving your code to preDispatch() hook as init() method should be used for helper initialization.
To get view instance for controller:
function getView()
{
$controller = $this->getActionController();
if($view = $controller->view) {
return $view;
}
if($this->getFrontController()->getParam('noViewRenderer') {
return $controller->initView();//this view instance will not be used in Zend_Layout!
}
$vr = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
return $vr->initView();
}
if you want to pass parameter to layout, then use
$view = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->initView();
Action helpers are called so because they are a way to avoid code duplication in actions. Everything you do in an action helper is available to the actions but not to the view. That's the normal behaviour of actions, as long as you don't pass something to the view, the view doesn't know it. If you want to avoid code duplication in your views, create view helpers.
Sometimes it can make sense to create an action helper and a corresponding view helper.
--
preDispatch works() while init() doesn't because with init you're not actually hooking into the dispatch process.