latata
latata

Reputation: 1723

Action and pass variable to all actions in symfony 1.4

I want to make some action (php script) before all actions in my frontend app and then pass a result from that script to actions in variable - so I can get variable value from all actions. Where should I declare sth like this?

Upvotes: 0

Views: 2017

Answers (2)

Visavì
Visavì

Reputation: 2333

If the filter solution dont feet your needs, you can also create a base action class with a preExecute function:

// app/frontend/lib/baseActions.class.php

class baseActions extends sfActions
{
   public function preExecute()
   {
      $this->myVar = .... // define your vars...
   }
}

Then your module actions class extends your baseActions class:

// app/frontend/modules/myModule/actions/actions.class.php

class myModuleActions extends baseActions
{
   public function executeIndex(sfWebRequest $request)
   {
      // var $this->myVar is available in any action and in your template
      ... 
   }
}

if you have to use the preExecute function in your module class action, remember to call parent::preExecute() in it.

Upvotes: 3

j0k
j0k

Reputation: 22756

What kind of information ?

I would recommend you to use filters.

In your apps/frontend/config/filters.yml:

rendering: ~
myfilter:
  class: myCustomFilter

Create the file lib/filter/myCustomFilter.php:

<?php
class myCustomFilter extends sfFilter
{
  public function execute ($filterChain)
  {
    if ($this->isFirstCall())
    {
      // do what ever you want here.
      $config = Doctrine_Core::getTable('Config')->findAll();
      sfConfig::set('my_config', $config);
    }

    $filterChain->execute();
  }
}

And then, every where, you can retrieve your data:

sfConfig::get('my_config');

Upvotes: 2

Related Questions