Accessing a controller action through out the application in Zend - php

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

Related

How to add new view to Phalcon app?

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
}
}

Passing data from one controller action to another - Magento

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);

Create form for several actions in Zend Framework

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.

Can Symfony2 controller and template expressions be written relative to the bundle?

Take for example the following controller/action:
public function indexAction()
{
return $this->render('TestBundle:TestController:index.html.twig');
}
I would like to write the template expression (or whatever it's name is) this way:
public function indexAction()
{
return $this->render('*:TestController:index.html.twig');
}
So that symfony knows I'm looking for a template in this very bundle. Having to write the whole Owner + Bundle for every template/action/repository I want to refer is very annoying. Even more so considering most of the time I refer to actions and templates in the same bundle.
NOTE: I know templates can be put at the app level and be refernced like this:
'::index.html.twig'
But that is not what I need.
It's possible with a bit of custom code.
Basically, you want to override the controller's render() method and include logic to fetch the name of the current bundle.
Note that instead of my controllers extending Symfony\Bundle\FrameworkBundle\Controller\Controller, they extend a custom controller (which then extends Symfony's controller). This allows you to conveniently give the controller more ability by adding your own methods.
Ex:
MyBundle\Controller\MyController\ extends MyCustomBaseController which extends Symfony\Bundle\FrameworkBundle\Controller\Controller.
So, in my custom controller I have these two methods:
public function render($view, array $parameters = array(), Response $response = null) {
$currentBundle = $this->getCurrentBundle();
$view = str_replace('*', $currentBundle, $view);
return parent::render($view, $parameters, $response);
}
public function getCurrentBundle() {
$controller = $this->getRequest()->attributes->get('_controller');
$splitController = explode('\\', $controller);
return $splitController[1];
}
Take a look at render(). It fetches the current bundle name and uses it to build the $view variable. Then it just calls parent::render() and it's as if you had manually defined the bundle name in the render statement.
The code here is very simple, so you should be able to easily extend it to do other things, such as allow you to also avoid typing the controller name.
Important: If you do use a custom controller, make sure you use Symfony\Component\HttpFoundation\Response, otherwise PHP will complain that the method signatures for render() don't match.

Zend:does action helper's init method passes variables to layout view?

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.

Categories