doddsey_65 Posted February 19, 2014 Share Posted February 19, 2014 Hi, I have started to create a project using Symfony 2. I need to share data between all controllers. I have added a base controller which extends symfony\controller and each of my controllers extends this base controller class BaseController extends Controller class HomeController extends BaseController This base controller will be used for things like assigning global twig variables ( I know I can do this in the config but some of the variables will be gotten from other config files and database ). So I thought I could reference container since Controller is container aware, however it isn't at the point I am using the functions (from constructor). public function __construct () I have seen people mention passing the container in as a parameter and mention services but I have had a look and cannot figure it out. All I want to achieve is this: public function __construct (Container $container) { $container->get('twig').addGlobal('foo'); } Quote Link to comment https://forums.phpfreaks.com/topic/286305-symfony-2-sharing-data-between-controllers/ Share on other sites More sharing options...
trq Posted February 19, 2014 Share Posted February 19, 2014 Have another look at the documentation on services. If your stuck, post some actual code. Quote Link to comment https://forums.phpfreaks.com/topic/286305-symfony-2-sharing-data-between-controllers/#findComment-1469472 Share on other sites More sharing options...
trq Posted February 19, 2014 Share Posted February 19, 2014 Oh and besides. Controllers should be pretty dumb. What you describe as a solution is likely the side effect of some other poor design decision. Quote Link to comment https://forums.phpfreaks.com/topic/286305-symfony-2-sharing-data-between-controllers/#findComment-1469474 Share on other sites More sharing options...
Solution doddsey_65 Posted February 19, 2014 Author Solution Share Posted February 19, 2014 Going to mark this as solved (it isn't). I was planning on moving all of my codebase over to Symfony but it seems like it would take a lot of effort and disrupt my current workflow. Quote Link to comment https://forums.phpfreaks.com/topic/286305-symfony-2-sharing-data-between-controllers/#findComment-1469492 Share on other sites More sharing options...
ignace Posted February 19, 2014 Share Posted February 19, 2014 Perhaps reading documentation instead of complaining: http://symfony.com/doc/current/cookbook/templating/global_variables.html Quote Link to comment https://forums.phpfreaks.com/topic/286305-symfony-2-sharing-data-between-controllers/#findComment-1469512 Share on other sites More sharing options...
doddsey_65 Posted February 19, 2014 Author Share Posted February 19, 2014 In which post did I complain? I mearly stated that it would take alot more work than I thought to migrate from Silex. The code in those docs uses the config files to store global variables and, as I said, I cannot do it that way and it has to be from a controller. Quote Link to comment https://forums.phpfreaks.com/topic/286305-symfony-2-sharing-data-between-controllers/#findComment-1469520 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.