0

Working with Zend Framework 1, I'm currently stucked with placeholders and really don't understand where is the issue.

I would like to render some content using a placeholder in my main view :

<div>
<?= $this->placeholder( 'segment' ); ?>
</div>

I have a main action in which I create the page, but I would like to set the render for this placeholder in another action, using ActionStack Helper. It seems to be impossible.

This line :

$this->view->placeholder('segment')->set( 'XXXXXX' );

give the expected result when written on the main action, but has no effect inside the action I call with ActionStack.

I've try to handle the placeholder in the view of my new action, and inside the code of the action. No result, my placeholder give me no output.

Any clues ?

(I work on a project which don't use Zend_Layout, I don't know if it may have a consequence)

Alexandre Brach
  • 325
  • 2
  • 12
  • Please explain further : Do you want the `new action` to set content for the `segment` placeholder that you want to be rendered in the `main action` output? – php-dev May 22 '14 at 13:18
  • yes, this is exactly what I want – Alexandre Brach May 22 '14 at 14:56
  • The problem can be, the view is your main.phtml view script which gets rendered after the mainAction was finished. So the placeholder is rendered when the mainAction is done. Changing to another Action after the mainAction, which is what the ActionStack Helper does, just not sending the response and finishing the dispatch process, but instead call the next action from the stack. Maybe you should prevent rendering th eview in the mainAction and instead render a view in your last action from your action stack. – Scriptlabs May 24 '14 at 20:05
  • In my mind, the views was rendered after the run of every actions. So I have a misunderstood of the routing MVC of Zend. Thank you for this. – Alexandre Brach May 26 '14 at 08:49

1 Answers1

0

There are some things with the Zend Action Stack Helper which are really bad and this is why i suggest you to not use this helper at all.

Here are some infos about why i suggest it:

http://www.rmauger.co.uk/2009/03/why-the-zend-framework-actionstack-is-evil/

You should really try another approach, like using a parent class with protected attribs you write during the dispatch process.

Maybe tell us what you want to do, and we can try to give you some other ideas and examples on how to solve your problem in a different way.

I will now add another solution

I think what you want to do is, building up some kind of information into a variable you can use in your view script.

Let's say you have an IndexController.php in you Application controllers folder.

class IndexController extends Zend_Controller_Action {
     //add a protected attrib to your class that can be used to transfer and store data
     protected $_segment = '';
     //you can also add setter and getter for this attribute         

     public function mainAction() {
          //you do some things here
          //$this->view->placeholder('segment')->set('xxxxx','test');
          //instead do this
          $this->_segment= 'This is a';
          //build your action stack and add anotherAction to your stack
     }
     public function anotherAction() {
          //$this->view->placeholder('segment')->set('xxxxx','test');
          //instead do the following

          $this->_segment.= ' test';
     }

    /**
     * Is executed before a controller Action is being executed.
     * 
     * @return
     */
    public function preDispatch() {

    }

    /**
     * Is executed after a controller Action is being executed.
     * 
     * @return
     */
    public function postDispatch() {
        //set a view variable at the end of dipatching process
        $this->view->segment = $this->_segment;
    }
}

In your main.phtml view script or in any other view script that gets rendered in this dispatching process, you can now use the constructed view variable like any other viwe variable.

echo $this->segment;
Scriptlabs
  • 498
  • 3
  • 16