Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8130321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:40:13+00:00 2026-06-06T08:40:13+00:00

I recently started a project in Symfony2 and I need to run some methods

  • 0

I recently started a project in Symfony2 and I need to run some methods before and after every action to avoid code redundancy (like preDispatch/postDispatch from Zend Framework and PreExecute/PostExecute from Symfony1).

I created a base class from which all the controllers are inherited,
and registered an event listener to run controller’s preExecute() method before running requested action, but after reading tons of documentation and questions from here I still can’t find how to run postExecute().

Foo/BarBundle/Controller/BaseController.php:

class BaseController extends Controller {
  protected $_user;
  protected $_em;

  public function preExecute() {
    $user = $this->get('security.context')->getToken()->getUser();
    $this->_user = $user instanceof User ? $user : null;
    $this->_em = $this->getDoctrine()->getEntityManager();
  }

  public function postExecute() {
    $this->_em->flush();
  }
}

Foo/BarBundle/Controller/FooController.php:

class FooController extends BaseController {
  public function indexAction() {
    $this->_user->setName('Eric');
    $this->_em->persist($this->_user);
  }
}

Foo/BarBundle/EventListener/PreExecute.php:

class PreExecute {
  public function onKernelController(FilterControllerEvent $event) {
    if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
      $controllers = $event->getController();
      if (is_array($controllers)) {
        $controller = $controllers[0];

        if (is_object($controller) && method_exists($controller, 'preExecute')) {
          $controller->preExecute();
        }
      }
    }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T08:40:14+00:00Added an answer on June 6, 2026 at 8:40 am

    There is a discussion of this here and this particular example by schmittjoh may lead you in the right direction.

    <?php
    
    class Listener
    {
        public function onKernelController($event)
        {
            $currentController = $event->getController();
            $newController = function() use ($currentController) {
                // pre-execute
                $rs = call_user_func_array($currentController, func_get_args());
                // post-execute
    
                return $rs;
            };
            $event->setController($newController);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've recently started on a project to develop some portlets which will run on
I recently started a new RoR project, and copied some of my old code
I've recently started a new project in C#, and, as I was coding some
I have recently started setting up the security for my Symfony2 project. I opted
Recently I started a project in Symfony2 from the BETA version available on symfony.com
I recently started a project and needed to do some integration with LDAP via
I have recently started working on a very large C++ project that, after completing
I recently started deploying my test code onto an actual device and ran some
I have a recently started project where i'm using mongoid for the database and
I currently have a recently started project in development with a 2010 target date

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.