How to get a controller inside plugin? - php

How can I access an action controller inside controller plugin?
I need to acces it in the preDispatch method.
class My_Controller_Plugin_MyPlugin extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$actionController = ?;
}
}

I know that I'm 2 years late to the party, but since I stumbled across this while looking for it myself I figured I'd post anyway. What you're looking for is:
//$module = $request->getModuleName(); // Not everyone uses modules.
$controller = $request->getControllerName();
$action = $request->getActionName();
Caveat... This is only giving you what was requested. If you change the request after dispatch this won't be accurate anymore.

Something like this should work:
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$actionController = $this->getActionController();
}
Edit
Actually this won't work in plugin (will work in action helper).
Some possible workarounds:
subclass Zend_Controller_Action
instead accessing the controller, access the plugin from controller using plugin broker
move plugin logic to action helper
store the data as bootstrap param
store the data in the Registry

Related

Cake 2.X How to refresh session at each action

Is there a way in CakePhp 2.X where at each beginning of a page my user's session is refreshed with the data in the database ?
For the moment, i could only make it work if the user is inside the UsersController :
public function beforeRender($user = null){
parent::beforeRender($user);
if(AuthComponent::user()){
$info = $this->User->findById( $this->Auth->User('id'));
$this->Auth->login( $info['User']);
}
}
But i have no clue on how to automate this action through the whole site since the User object only exists in my UsersController.
I'm new to cakePhp so sorry if i miss some concepts.
Move the logic to the beforeRender callback of your AppController and remember to make sure that you've loaded the User model that you want to query:-
public function beforeRender() {
parent::beforeRender();
if (AuthComponent::user()) {
$this->loadModel('User');
$info = $this->User->findById( $this->Auth->User('id'));
$this->Auth->login($info['User']);
}
}
Also note that the beforeRender method takes no parameters.
I'd move this function to the AppController (which all controllers should inherit from), this would then fire on every page lifecycle

Zend framework passing data from plugin to the controller

From one of my controller plugin, I want to pass a value to the controller. I have tried it using the Zend_Registry functionality. But it is not working.
In my plugin, I have tried setting registry key as follows.
Zend_Registry::set('msg','hello world!');
echo Zend_Registry::get('msg');
In my controller's action method, I tried to access it as follows
Zend_Registry::get('msg');
but nothing returns. In plugin it sets the value and outputting it. Am i missing anything? Or is this not a right way to do this? If so can someone help me?
plugin below
class COM_Application_Controller_Plugin_TestPlugin extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$currentController = Zend_Controller_Front::getInstance()
->getRequest()
->getControllerName();
$moduleName = $request->getModuleName();
Zend_Registry::set('msg','Hello Word!');
}
}

Zend Plugin , register during predispatch but before action method is called

I am trying to figure a way to call a plugin during predispatch, but I am having trouble calling it last.
Basically I need to call this after all controller predispatch, before the action method is called.
Is this possible?
I tried calling the plugin lastly by passing a high value, but I know this is wrong. I am not looking to call this on the postDispatch.
$front->registerPlugin(new Plugin_Acl, 1000);
class Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(...)
{
// ... DO STUFF before any action method
}
}
The standard Zend_Controller_Action has an empty preDispatch() method which is called after preDispatch() on all front-controller plugins (which occurs before the controller is even instantiated) and then on all attached action-helpers, right before the action method itself. See the dispatch() methods on Zend_Controller_Front, Zend_Controller_Dispatcher_Standard, and Zend_Controller_Action for the flow.
So, if you want something to run at that point on all controllers, then you could create a base controller with a preDispatch() method containing the code you want to run.
I am doing that in dispatchLoopStartup()
class My_Plugins_Front extends Zend_Controller_Plugin_Abstract {
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request){
// Register ACL plugin if is admin controller (for eg.)
if($request->getControllerName() == 'admin'){
/**
* Set up your ACL
*/
}
// You can setting variable to see in all view files
$layout = Zend_Layout::getMvcInstance();
$view = $layout->getView();
$view->test = 'test';
}
public function dispatchLoopShutdown(){}
}
calling in predispatch seems to work for my acl:
class Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request) {
parent::preDispatch($request);
//do some stuff
}
Then it instantiated in the application.ini with the line:
resources.frontController.plugins.acl = "Controller_Plugin_Acl"
Hope this helps some...

Accessing a controller action through out the application in Zend

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

Zend Framework: Getting request object in bootstrap

How do I get the request object from inside the bootstrap file?
I can try this methods but not work.
$request= new Zend_Controller_Request_Http();
$request = Zend_Controller_FrontController::getInstance()->getRequest();
If you really want to, you may achieve this calling:
public function _initRequest()
{
$this->bootstrap('frontController');
$front = $this->getResource('frontController');
$front->setRequest(new Zend_Controller_Request_Http());
$request = $front->getRequest();
}
However, this should be avoided, because the most data you need from the Response object will be available after the front controller is dispatched (eg. module, controller or action name).
The other variables stored in the Response object are extracted from global arrays such as $_SERVER, $_POST or $_GET which you may exceptionally read directly in bootstrap.
But most likely, you want to use Response object in front controller plugin:
class Your_Controller_Plugin_PluginName extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
// do anything with the $request here
}
}
You shouldn't get the request objet, since if you see the dispatch loop, the idea is that the bootstrap are actions prior to execute in a request.
If you need to alter someway of the application use a Controller Plugin to do that.
You need to bootstrap the frontController first, try something like:
function initFoo()
{
$this->bootstrap('frontController');
$req = $this->frontController->getRequest();
}

Categories