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 7992009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:27:15+00:00 2026-06-04T13:27:15+00:00

I have a base controller for most of the controllers in my app like

  • 0

I have a base controller for most of the controllers in my app like so:

class BaseController extends \Symfony\Bundle\FrameworkBundle\Controller\Controller
{
    /**
     * 
     * @Route("/")
     */
    public function indexAction($partial = false)
    {
            $this->partial = $partial;
        $this->currentAction = 'index';

        return $this->r();
    }
}

This is accompanied by a pack of templates that can be either full html pages with a layout or just the content. This is done by a line in the templates:

{% extends this.partial ? "SomeProject:_base:partial.html.twig" : "SomeProject::layout.html.twig" %}

(where this is a the controller reference).

These templates can then be rendered in other controllers without the duplication of layout via.

{% render 'SomeProject:SomeController:index' with  { "partial":true }  %}

My problem with this approach is:

  1. Every action that needs to be partial controller must have a $partial argument. Since most actions have the potential to be partial, all the controllers must be sprinkled with it.
  2. Every potentially partial action must have the $this->partial = $partial line, which can be easily forgotten.

It there a cleaner way by using some Symfony or Twig magic (overriding the render tag etc. ). For getting rid of the above problems?

  • 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-04T13:27:17+00:00Added an answer on June 4, 2026 at 1:27 pm

    After some research and digging around in the internals I’ve come up with an elegant solution.

    The answer is to build an Event Listener (covered in Symfony2 Docs). More precisely: a Controller Listener with the meat of the class looking like below. This allows for transparent handling of partial without any changes made to the Controller code.

    class ControllerListener
    {
        /**
         *
         * @param BaseController $ctrl
         * @param Request $request
         * @return BaseController 
         */
        public function injectPartial($ctrl,Request $request)
        {
            if ($ctrl instanceof BaseController)
            {
                if ($request->get("partial",false))
                    $ctrl->setPartial($request->get ("partial"));
            }
            return $ctrl;
        }
    
        public function onKernelController(FilterControllerEvent $event)
        {
            $ctrl = $event->getController();
            $ctrl[0] = $this->injectPartial($ctrl[0], $event->getRequest());
            $event->setController($ctrl);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class looking like this: public class BaseController : Controller {
I have abstract BaseController, which basically looks like below: public abstract class BaseController :
I have made a base controller class that overrides OnActionExecuting as follows: public class
I have a base controller class and I would like to pass a Message
I have a base class, defined as below (I'm also using DevExpress components): public
I have a base controller class that attempts to look at Request.ContentType to see
I have created a Base-Controller from which all the controllers inherit. Currently this controller
Trying to avoid repetition here. I have an action in a base class controller
I have a base controller from which every other controller inherits. I would like
I have a library base class ( Controller ) and three sub-classes that inherit

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.