Strange behavior of the Controller_Plugin - zend-framework

In this example I want increase the session variable called "test" by one each time it enter to the controller. If comment the content of the method preDispath works fine but with this precise example, the session variable "test" increase in 3 or 5 each time.
I use Zend Framework 1.11.4
Why??? I hope you understand my question.
Remember this example is only to show the strange behavior of the method preDispatch
My plugin
class App_Plugins_Permisos extends Zend_Controller_Plugin_Abstract{
public function __construct(){}
public function preDispatch(Zend_Controller_Request_Abstract $request) {
$request->setModuleName('default');
$request->setControllerName('index');
$request->setActionName('index');
}
}
My bootstrap
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap{
protected function _initSession(){
Zend_Session::start();
}
protected function _initPlugins(){
$this->bootstrap('frontcontroller');
$this->frontController->registerPlugin(new App_Plugins_Permisos());
}
}
My Controller
class IndexController extends Zend_Controller_Action{
public function init(){}
public function indexAction(){
$s = new Zend_Session_Namespace('test');
if(isset($s->test)){
$s->test++;
}else{
$s->test = 1;
}
Zend_Debug::Dump($s->test);
die();
}
}
Thanks a lot

Try it putting in .ini resources.frontController.plugins.foo = "My_Plugin_Foo"
If works tell me! Tnks

Related

How to use preDispatch variables in my controller

I am creating custom plugin in zend framework using Zend_Controller_Plugin_Abstract in the disptach method i am using the following code
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
global $serversetting;
$serversetting = 'S3Server';
}
Now I want to use the value of $serversetting inside my controller
How Can i use them??
Thanks In Advance !!
class MyclassController extends Zend_Controller_Action
{
private $_serversetting;
public function preDispatch(Zend_Controller_Request_Abstract $request) {
$this->_serversetting = 'S3Server'; //set
}
[...]
public function otherAction() {
[...]
$foo = $this->_serversetting; //get
[...]
}
}

Global variable in Zend Controller

class UserController extends Zend_Controller_Action
{
protected $varGlobal;
public function oneAction()
{
$this->varGlobal=0;
.........
}
public function twoAction()
{
$temp=$this->varGlobal;//temp return null;
}
}
how do i keep value of varGlobal after excuted oneAction()?
Thanks!
You could also try static properties :
class UserController extends Zend_Controller_Action
{
protected static $varGlobal;
public function init(){
self::$varGlobal=0;
}
public function oneAction()
{
self::$varGlobal=15;
.........
}
public function twoAction()
{
$temp=self::$varGlobal;//temp return 0,don't change value after excute oneaction();
}
}
Well, $this->varGlobal persists as long as you call twoAction method on same object. For every request new object gets created. so if your next request goes to twoAction, one action won't gets fired.
In order to set the variable for every object, you could use init function and initialize variables there.
Why don't use Zend_Controller_Action::_setParam() and Zend_Controller_Action::_getParam() ?
or Zend_Registry ?

Zend Framework: How to render a different action?

I have two actions that are essentially identical, but need different URLs. Normally I would use _forward() to render the other action:
class MyController extends Zend_Controller_Action
{
public function actionOneAction()
{
$this->_forward('action-two');
}
public function actionTwoAction()
{
$this->view->foobar = 'foobar';
}
}
However, I have some code the is happening in preDispatch() that I only want to execute once:
class MyController extends Zend_Controller_Action
{
public function preDispatch()
{
//execute this only once before actionOne or actionTwo, but not both
}
public function actionOneAction()
{
$this->_forward('action-two'); //this won't work because preDispatch() will get called a second time
}
public function actionTwoAction()
{
$this->view->foobar = 'foobar';
}
}
So I thought maybe I could simply call the function directly, like this:
class MyController extends Zend_Controller_Action
{
public function preDispatch()
{
//execute this only once before actionOne or actionTwo, but not both
}
public function actionOneAction()
{
$this->actionTwoAction(); //execute the same code as actionTwoAction()
}
public function actionTwoAction()
{
$this->view->foobar = 'foobar';
}
}
But now Zend Framework is complaining about not being able to find the action-one.phtml view script. I don't want to render actionOne's view script. I want to render actionTwo's view script. What do I need to do?
Using render() seems to do the trick:
public function actionOneAction()
{
$this->actionTwoAction(); //execute the same code as actionTwoAction()
$this->render('action-two'); //renders the same view as actionTwoAction()
}

How to get resource in controller action?

How to get resource in controller action?
Resource db was initialized in application.ini.
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// I want db resource here
}
}
Try and see if this works:
$this->getFrontController()->getParam('bootstrap')->getResource('db')
UPDATE : While this solution works, it is NOT a recommended
practice. Please, read comment by
#Brian M. below.
You can use Zend_Registry. Initialize the database connection in the bootstrap and store it in the registry:
// set up the database handler
// (...)
Zend_Registry::set('dbh', $dbh);
Then you can retireve it from anywhere else:
$dbh = Zend_Registry::get('dbh');
In answer to a similar question on Nabble, Matthew Weier O'Phinney (Mr Zend Framework 1) suggests using this form:
$this->getInvokeArg('bootstrap')->getResource('db');
So, in the context of this question, it would be something like:
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// db resource here
$db = $this->getInvokeArg('bootstrap')->getResource('db');
}
}

zend framework can't find Model classes?

Yall:
I have a simple question, it might be a simple configuration issue, but
I have a Model defined, and when I try to access it from a controller it
fails.
The Model is in the model directory, and when I look at the quickstart app,
it seems like this should work.
Here is my model:
<?php
class Application_Model_User {
protected $_user;
protected $_password;
protected $_userId; // very simple right
}
?>
My controller just stops.. here is the controller code:
<?php
class UserController extends Zend_Controller_Action {
public function init() {
}
public function indexAction() {
// display login form
$users = new Application_Model_User();
echo "test never echos.. stopped above ? weird huh.."; // fails before ..
}
?>
Thank you everyone,
In application.ini
appnamespace = "Application_"
App structure:
/application/
/models/
/User.php
Class definition:
class Application_Model_User {}
Should work OK.