Option 1: Check the environment in the controller
public function testAction(Request $request)
{
$env = $this->container->get( 'kernel' )->getEnvironment()
if ($env !== 'dev') {
throw $this->createAccessDeniedException();
}
//your action
}
Option 2: Only include the route in development environment
For this options, let me rephrase your question: How to enable controllers in development environment? (instead of disable in production)
Take a look at the Symfony Standard Edition, "a fully-functional Symfony application that you can use as the skeleton for your new applications.". It features a dev environment that includes routes for WebProfilerBundle (a.k.a. Web Debug Toolbar).
In your dev
environment, config_dev.yml is being loaded. You can define a routing file that extends the main router:
config_dev.yml
framework:
router:
resource: '%kernel.project_dir%/app/config/routing_dev.yml'
strict_requirements: true
profiler: { only_exceptions: false }
routing_dev.yml
test:
resource: '@TestBundle/Controller/'
type: annotation
_main:
resource: routing.yml