Can't use flash method in prod environment

2019-07-17 07:37发布

问题:

I'm using the setFlash and hasFlash methods of symfony 1.4 with WAMP 2.0

Locally with my frontend_dev app, all work fine. But in production environment, my test $this->forward404Unless($user->hasFlash('resultsArray')); fails.

I thought that the flash methods where enabled by default. What can I do to make it works please ?

Edit : I found an interesting error message.

Here is my filters.yml file

# You can find more information about this file on the symfony website:
# http://www.symfony-project.org/reference/1_4/en/12-Filters

rendering: ~
security:  ~

# insert your own filters here

cache:     ~
flash:     ~
execution: ~

In the frontend_prod.log, I have :

Mar 16 05:57:42 symfony [info] {sfPatternRouting} Match route "homepage" (/) for / with parameters array ( 'module' => 'main', 'action' => 'index',)
Mar 16 05:57:42 symfony [err] {sfParseException} Configuration file "D:\wamp\bin\php\symfony\symfony14\lib/config/config/filters.yml" specifies category "flash" with missing class key.

回答1:

Do you call the executeShowResult action in a redirect/forward context? The flash has a limited lifetime and it will disappear after the very next request. Clearly the resultsArray has been flushed out.



回答2:

You have a 404 being generated in production (favicon or image) and that counts as a request to the 404 action (after a redirect) clearing out your flash variables.



回答3:

Not sure if this is the answer, but I don't have a flash key in my filters.yml file, in a Symfony 1.4.3 app, and I haven't touched it since grabbing the version from the Symfony site. Have you tried removing that line from the yml file, clearing your cache and trying again?



回答4:

I wonder if you have caching enabled for prod?

If you do, and are caching with_layout (or if flash is in the action template, then caching the relevant action), then it will returned the cached page without the flash message on subsequent views.



回答5:

Be sure you have the use_flash=true on your factories.yml

user:
  class: myUser
  param:
    timeout:         3600
    logging:         %SF_LOGGING_ENABLED%
    use_flash:       true
    default_culture: %SF_DEFAULT_CULTURE%    

A pretty old response but I hope is of some use to anybody else with the same problem.



回答6:

@Lunohodov

(Sorry for posting an answer instead of a comment but I can't use this way cause of the characters limitation)

Here is the action :

public function executeShowResult()
    {
        $user = $this->getUser();
        $this->forward404Unless($user->hasFlash('resultsArray'));

        $this->results = $user->getFlash('resultsArray');
        $user->setFlash('resultsArray', $this->results);

        $this->pager = new myArrayPager(null, 15);
        $this->pager->setResultArray($this->results);
        $this->pager->setPage($this->getRequestParameter('page'));
        $this->pager->init();

        $this->myModule = $this->getRequestParameter('myModule');
        $this->myTemplate = $this->getRequestParameter('myTemplate');

        $forwardPage = '../../'.$this->getRequestParameter('myModule').'/templates/'.$this->getRequestParameter('myTemplate');
        $this->setTemplate($forwardPage);

        return sfView::SUCCESS;

    }

Edit : Another interesting log. In my action which set the flash, I've put a log to test the set and hasFlash methods... and it worked :

$user = $this->getUser();
$user->setFlash('resultsArray', $this->results);
if ($user->hasFlash('resultsArray'))
{
    sfContext::getInstance()->getLogger()->info("The flash is set");
}
else
{
    sfContext::getInstance()->getLogger()->info("The flash is NOT set");
}

The logs for that :

Mar 16 06:07:13 symfony [info] The flash is set

I think I'm missing something big here...