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.
Related
I'm completely confused on how Phalcon PHP renders its views. I want to create a new page called "manager".
From my understanding by creating a controller I can link it to a view. I create a controller called ManagerController.php and then added a view in views/manager/index.volt.
I added a bit of text the volt file to check if it works. When I go to /manager/ nothing shows up.
Am I doing this correct or do I have to assign a view somewhere?
class ManagerController extends ControllerBase
{
public function initialize()
{
$this->tag->setTitle('Files/My Files');
parent::initialize();
}
}
The initialize function on a controller is an event ran after constructing the controller
In order to display view for that controller it is necessary to at least setup an index action
In your you are interested in rendering a route of /manager/ , this will correspond to indexAction
class ManagerController extends ControllerBase
{
public function initialize()
{
$this->tag->setTitle('Files/My Files');
parent::initialize();
}
public function indexAction()
{
// This will now render the view file located inside of
// /views/manager/index.volt
// It is recommended to follow the automatic rendering scheme
// but in case you wanted to render a different view, you can use:
$this->view->pick('manager/index');
// http://docs.phalconphp.com/en/latest/reference/views.html#picking-views
}
// If however, you are looking to render the route /manager/new/
// you will create a corresponding action on the controller with RouteNameAction:
public function newAction()
{
//Renders the route /manager/new
//Automatically picks the view /views/manager/new.volt
}
}
I have a custom module with a controller action which performs a certain function,
class Company_CustomModule_ActionController extends Mage_Core_Controller_Front_Action
{
public function doAction()
{
// do something
}
}
I have another controller in the same module (lets say "test"). I would like to call an action within this "test" controller in the above mentioned controller and pass it a parameter like,
class Company_CustomModule_ActionController extends Mage_Core_Controller_Front_Action
{
public function doAction()
{
// do something
// call the index in the "Test" controller
Mage::app()->getFrontController()->getResponse($data)
->setRedirect(Mage::getUrl('company/test'))
->sendResponse();
}
}
My goal is to pass some data from the Action controller to the Test Controller and execute the index action in the Test Controller from within the Action Controller.
Note: in essence, I would like to pass on the POST data received by the Action Controller to the Test Controller.
How would I go about doing this? any guidance would he helpful.
I'm not overly familiar with Magento, but since it's built on the Zend Framework you should be able to use the forward helper and pass your data in the params parameter:
return $this->_forward('test', 'company', $module, $params);
I have an upload form, which is displayed on overlay/lightbox/toplayer. It's available only for several actions in several controllers.
Creating this form needs ~6 lines of code and needs access to REQUEST object to grab params from it.
Where should I put this code, so I could easily create my form in actions I need.
I'd put this functionality in an Action Helper. An action helper can be called directly from any controller action (and is lazy loaded) and action helpers have access to everything the controller action does including the request object and view.
Example:
<?php
class My_Action_Helper_FormCreator extends Zend_Controller_Action_Helper_Abstract {
public function direct($options = null)
{
$request = $this->getRequest();
$view = $this->getActionController()->view;
$form = new Application_Form_SomeForm();
// set form options here...
$view->form = $form; // optional - assign form directly to the view
return $form;
}
}
Place that code in library/My/Action/Helper/FormCreator.php (or where desired and change the class name).
Then, in your actions, call it like this:
$form = $this->_helper->FormCreator();
Lastly, we need to tell the helper broker where to find this action helper. To do so, add this to your bootstrap:
protected function _initActionHelpers() {
Zend_Controller_Action_HelperBroker::addPrefix('My_Action_Helper');
}
Hope that helps.
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
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];
}