Zend Unset Action helper from controller action - php

Zend framework talk.I'm initializing in my bootstrap class My_Action_Helper_Custom (extending Zend_Controller_Action_Helper_Abstract) to make it available to all of my controllers.
Could I just disable it for a specific action where I dont need it?
thanks
Luca

Are you referring to disabling the preDispatch() or postDispatch() hooks for a particular controller action?
If so, I'd add some form of blacklist property to the helper, for example
/**
* #var array
*/
private $blacklistActions = array();
public function addBlacklistAction($action)
{
// store actions in string form
// eg, module.controller.action
$this->blacklistActions[] = $action;
}
public function preDispatch()
{
$request = $this->getRequest();
$action = sprintf('%s.%s.%s',
$request->getModuleName(),
$request->getControllerName(),
$request->getActionName());
if (in_array($action, $this->blacklistActions)) {
return;
}
// the rest
}

Related

Zend 1 plugin before set routing

I need get plugin before load routes. I use routeStartup and preDispatch in plugin, but it doesn't help.
class Base_Controller_Plugin_Website extends Zend_Controller_Plugin_Abstract
{
public function routeStartup(Zend_Controller_Request_Abstract $request)
{
Base_Website::setRequest($request);
}
}
I need method from Base_Website.
The earliest front-controller plugin event is routeStartup, so if you want to perform some action prior to that, you'll need to do it in Bootstrap.
Unfortunately, the methods that run during bootstrap don't pass the Request and Response objects to you. You'll have to dig them out yourself. Something like:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
// all your other _initXXX() bootstrap methods
// etc...
protected function _initSomethingUsingRequest()
{
$this->bootstrap('frontController');
$front = $this->getResource('frontController');
$request = $front->getRequest();
// Now do whatever you want with your Request object
// etc...
}
}

sending data from action helper to partial view

i want to send some data from Action Helper to view Partial and i am unable to do it, to get the clear picture. here is all the related code i am using.
in my layout.phtml i am using this placeholder. to generate onDemand navigation menu.
<?php echo $this->placeholder('action-navigation'); ?>
so when i need it in my controller or action method i can simply place use this code.
$this->_helper->navigation()->renderActionNavigation();
The Action helper i am using is.
class Zend_Controller_Action_Helper_Navigation extends Zend_Controller_Action_Helper_Abstract
{
private $_view = null;
public function direct()
{
$this->_view = $view = Zend_Layout::getMvcInstance()->getView();
$this->_view->placeholder('action-navigation');
return $this;
}
public function renderActionNavigation()
{
$config = new Zend_Config_Xml(
APPLICATION_PATH.'/configs/navigation.xml', strtolower(
$this->getRequest()->getControllerName().
$this->getRequest()->getActionName()
)
);
$container = new Zend_Navigation($config);
// here i want to send $container to _action-navigation.phtml.
$this->_view->addScriptPath(APPLICATION_PATH.'/layouts/')->render('partials/_action-navigation.phtml');
}
}
this is my view partial _action-navigation.phtml
$this->placeholder('action-navigation')->captureStart();
//i want to get zend_navigation instance. from the above action helper here.
$this->placeholder('action-navigation')->captureEnd();
i have problem sending data from action helper to partial view _action-navigation.phtml how do i do it?
Thank you.
Use partial() instead of render():
$this->_view->partial('partials/_action-navigation.phtml', array('nav' => $container));
And in your partial:
$this->nav // to get your container

Kohana Sessions Issue

Let me try to explain what I want to do here. I am trying to re-write a pet project from Codeigniter 2.x to Kohana 3.2.x.
I have created a Site Template controller (below)
class Controller_Site_Template extends Controller_Template
{
public $template = 'templates/hero';
/**
* The before() method is called before your controller action.
* In our template controller we override this method so that we can
* set up default values. These variables are then available to our
* controllers if they need to be modified.
*/
public function before()
{
parent::before();
if ($this->auto_render)
{
// Initialize empty values
$this->template->title = '';
$this->template->content = '';
$this->template->session = '';
$this->template->styles = array();
$this->template->footer_scripts = array();
$session = Session::instance();
$this->template->session = $session;
}
}
/**
* The after() method is called after your controller action.
* In our template controller we override this method so that we can
* make any last minute modifications to the template before anything
* is rendered.
*/
public function after()
{
if ($this->auto_render)
{
$styles = array(
'assets/css/style.css' => 'screen',);
$footer_scripts = array(
'assets/js/libs/jquery-1.7.1.min.js',
'assets/js/application.js',
);
$this->template->styles = array_merge( $this->template->styles, $styles );
$this->template->footer_scripts = array_merge( $this->template->footer_scripts, $footer_scripts );
}
parent::after();
}
After the login form is submitted I set the session data and I am able to retrieve the session data in the Controllers that extend the Controller_Site_Template but I am unable to retrieve the session data in any of the View files.
The only way I am able to get the session data in the view files is to pass the session data in each controller that extends the Template_Site_Template:
$this->template->content->set_global('session',$this->template->session->as_array());
Is there an easy way to establish and set the session in the template_controller that can be used in all of the controllers, modelc, views rather that using the set_global on each individual controller?
I don't know if I am explaining this well but I am used to the ease of Codeigniter's $this->session->userdata(); function that can be called in any controller, model, and view once it was set.
Thank you in advance for any input on what I am doing incorrectly.
You can set or bind global data to your views with the following
View::bind_global('session', $session);
View::set_global('session', $session);
If you plan to change any data further along the application logic, then use bind.
If no more changes to the data are required, use set.
Edit: oh, the above is just for views and you want it across the entire application.
Just use the Session::instance()->set() and Session::instance()->get() as required across your application rather then assigning it in your application controller.

Yii's magic method for controlling all actions under a controller

Commando need's help from you.
I have a controller in Yii:
class PageController extends Controller {
public function actionSOMETHING_MAGIC($pagename) {
// Commando will to rendering,etc from here
}
}
I need some magic method under Yii CController for controlling all subrequest under /page || Page controller.
Is this somehow possible with Yii?
Thanks!
Sure there is. The easiest way is to override the missingAction method.
Here is the default implementation:
public function missingAction($actionID)
{
throw new CHttpException(404,Yii::t('yii','The system is unable to find the requested action "{action}".',
array('{action}'=>$actionID==''?$this->defaultAction:$actionID)));
}
You could simply replace it with e.g.
public function missingAction($actionID)
{
echo 'You are trying to execute action: '.$actionID;
}
In the above, $actionID is what you refer to as $pageName.
A slightly more involved but also more powerful approach would be to override the createAction method instead. Here's the default implementation:
/**
* Creates the action instance based on the action name.
* The action can be either an inline action or an object.
* The latter is created by looking up the action map specified in {#link actions}.
* #param string $actionID ID of the action. If empty, the {#link defaultAction default action} will be used.
* #return CAction the action instance, null if the action does not exist.
* #see actions
*/
public function createAction($actionID)
{
if($actionID==='')
$actionID=$this->defaultAction;
if(method_exists($this,'action'.$actionID) && strcasecmp($actionID,'s')) // we have actions method
return new CInlineAction($this,$actionID);
else
{
$action=$this->createActionFromMap($this->actions(),$actionID,$actionID);
if($action!==null && !method_exists($action,'run'))
throw new CException(Yii::t('yii', 'Action class {class} must implement the "run" method.', array('{class}'=>get_class($action))));
return $action;
}
}
Here for example, you could do something as heavy-handed as
public function createAction($actionID)
{
return new CInlineAction($this, 'commonHandler');
}
public function commonHandler()
{
// This, and only this, will now be called for *all* pages
}
Or you could do something way more elaborate, according to your requirements.
You mean CController or Controller (last one is your extended class) ?
If you extended CController class like this:
class Controller extends CController {
public function beforeAction($pagename) {
//doSomeMagicBeforeEveryPageRequest();
}
}
you could get what you need

Zend Framework url redirect

<?php
class PI_Controller_Plugin_AssetGrabber extends Zend_Controller_Plugin_Abstract
{
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
/*
The module name
*/
$moduleName = $request->getModuleName();
/*
This modules requires the user to be loggedin in order to see the web pages!
*/
$loginRequiredModules = array('admin');
if (in_array($moduleName,$loginRequiredModules)) {
$adminLogin = new Zend_Session_Namespace('adminLogin');
if (!isset($adminLogin->loggedin)) {
/*--------------------------------------
Here I want to redirect the user
*/
$this->_redirect('/something');
}
}
}
}
I'm trying to do a redirect $this->_redirect('/something') but doesn't work! Do you know how can I do a redirect in this case?
Best Regards,
... rest of code
if (!isset($adminLogin->loggedin)) {
$baseUrl = new Zend_View_Helper_BaseUrl();
$this->getResponse()->setRedirect($baseUrl->baseUrl().'/something');
}
... rest of code
<?php
class AlternativeController extends Zend_Controller_Action
{
/**
* Redirector - defined for code completion
*
* #var Zend_Controller_Action_Helper_Redirector
*/
protected $_redirector = null;
public function init()
{
$this->_redirector = $this->_helper->getHelper('Redirector');
}
public function myAction()
{
/* Some Awesome Code */
$this->redirector('targetAction', 'targetController');
return; //Never reached!
}
}
You need to get the redirector helper, then you can define the targetAction and targetController with the redirector. That should do it.
Either use Zend_Controller_Action_HelperBroker to get the redirect helper or do the redirect directly from the Request object.
See the examples given in
Redirect in Front Controller plugin Zend

Categories