Reputation: 185
Is it possible to store data in one private variable of a controller (Symfony2)?
One example:
/**
* Class CatsController
*
* @Route("cats")
* @Cache(expires="+600 seconds", public=true)
* @package oTeuGato\AppBundle\Controller
*/
class CatsController extends Controller {
/**
* @var $advertisements Advertisement[]
*/
private $advertisements;
/**
* Index advertisements page
*
* @Route("", name="oTeuGato_Cats")
* @Method("GET")
* @return Response
*/
public function indexAction()
{
$this->advertisements = ....(Use a service for gets advertisements)
}
/**
* Index advertisements by page
*
* @Route("/{id}", requirements={"id" = "\d+"}, defaults={"id" = 1}, name="oTeuGato_Cats_ByPage")
* @Method("GET")
* @return Response
*/
public function indexByPageAction(){
....
}
In this example whenever someone calls the URL: cats/1 in the controller I need them to have all advertisements of the previously called method (/cats).
Is this possible?
Note: I enabled the cache in the app.php file and app_dev.php.
Thanks for help and sorry for my English ;)
Upvotes: 0
Views: 484
Reputation: 5066
Symfony doesn't provide a mechanism for what you are describing. But any solution that would work for PHP more generally, will work for Symfony.
It depends if you want to remember advertisements for each user or for all users. If you want to remember it for each user, use sessions as Gareth Parker suggested. If you want to remember it for all users, then you would need APC user caching, memcache or another memory-based key-value store.
You may also have luck using Doctrine result cache. See http://doctrine-orm.readthedocs.org/en/latest/reference/caching.html
Upvotes: 1
Reputation: 5062
No, it's not. Not like that, anyway. What you want is to use sessions instead. Sessions are what you use to store variables between requests. Here are some examples
Upvotes: 0