Zend Framework: How to render a different action? - zend-framework

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

Related

GWT JsInterop - extending a Java interface in JavaScript

So, we are trying to use JsInterop so that in our GWT application, we can use some modules created externally in JavaScript.
We have an interface that have some contract, some methods that must be implemented by all views.
For example:
package com.foo;
import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsType;
#JsType(namespace = JsPackage.GLOBAL)
public interface MyView {
void doSomething();
void doSomethingElse();
Element getElement();
}
Now imagine that in the JS side, we have the implementation of that interface:
export class MyViewImplementation extends window.$wnd.MyView {
constructor() {
super();
}
doSometing() {
//Implementation goes here
}
doSomethingElse() {
//Implementation goes here
}
getElement() {
var div = document.createElement("div");
div.textContent = "External Component Test";
return div;
}
}
Can this work?
Currently, I have an error which says:
Class extends value undefined is not a constructor or null
Based on your feedback to my comment, I would remove #JsType from your interface and introduce a class with #JsType(isNative=true) that implements the interface.
public interface MyView {
void doSomething();
void doSomethingElse();
Element getElement();
}
public class JavaView implements MyView {
// . . .
}
#JsType(isNative = true, namespace = JsPackage.GLOBAL)
public class JsView implements MyView {
#Override
public native void doSomething();
#Override
public native void doSomethingElse();
#Override
public native Element getElement();
}
Then in JavaScript:
export class JsView {
// . . .
}

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 ?

Strange behavior of the Controller_Plugin

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

Referencing variable set by application in models (a good idea?)

i am using zend framework 1.10 with doctrine 2. i wonder if in my (doctrine) model class, isit a good idea to reference a variable set by my application (bootstrap.php, variable stored in Zend_Registry, i think its something like a global variable)
what i want to access is the doctrine entityManager. also i want the id of the logged in user
I am building a project with similar setup (ZF 1.10 + Doctrine2) and I've used dependency injection to deal with this situation, much like takeshin said. Here goes full project repository URL: https://bitbucket.org/phpfour/zf-doctrine2. Below are some code excerpts.
Here's my controller:
<?php
require_once APPLICATION_PATH . "/models/PostManager.php";
class IndexController extends Zend_Controller_Action
{
private $_em;
public function init()
{
$this->_em = $this->getInvokeArg('bootstrap')->getResource('doctrine');
}
public function indexAction()
{
$pm = new PostManager($this->_em);
$this->view->posts = $pm->getPublicPosts();
}
My entity manager (or service class):
<?php
class PostManager
{
protected $_em;
public function __construct(Doctrine\ORM\EntityManager $em)
{
$this->_em = $em;
}
public function getPublicPosts()
{
$query = $this->_em->createQuery('SELECT p FROM Entities\Post p WHERE p.visible = true');
$posts = $query->getResult();
return $posts;
}
Hope this helps!
you should simply use Zend_Auth for the logged-in-userId problem, then could do something like the following in your model
class Model extends BaseModel
{
public function something()
{
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
$loggedInUserId = $auth->getIdentity()->id;
}
}
}
There is nothing wrong with this approach (unless you are referring to singletons). Use dependency injection where possible.
However I'd create a service (or two) for this.
class Modulename_Services_Servicename
{
public function getCurrentUser() { ... }
public function getCurrentUserModel() { ... }
public function isLogged() { ... }
public function authenticate() { ... }
public function getSomeData()
{
$user = $this->getCurrentUser()
$model = new YourModel($user);
$query = ....;
$result = $query->execute();
return $result;
}
public function getSomeMoreData($usermodel) { ... }
}