3

Is it possible to "disable" specific actions when the prod environment is active?

I have a few test actions which shouldn't be executed in a production environment.

class TestController extends FOSRestController
{
    /**
     * @Rest\Get("/api/test", name="api_test")
     */
    public function testAction(Request $request)
    {
        // something
        return;
    }
}
yivi
  • 42,438
  • 18
  • 116
  • 138
StockBreak
  • 2,857
  • 1
  • 35
  • 61
  • 4
    I don't know of a way in annotations, I've always preferred to use the config files - if you use those you could just add the dev routes in `routing_dev.yml` – JimL Nov 22 '17 at 16:03
  • 1
    Or check the environment: https://stackoverflow.com/questions/10640866/accessing-the-appkernel-environment-variable-in-symfony-2 – Stephan Vierkant Nov 22 '17 at 22:06
  • @JimL ho can I do it? I am using routing annotations. – StockBreak Nov 23 '17 at 10:56
  • Um, I specifically said I did not know a way to do it with annotations – JimL Nov 25 '17 at 15:51
  • @JimL yes I know, sorry I meant "How can you you do that without annotations?". Thanks. – StockBreak Nov 26 '17 at 15:36
  • Like I said, load routes from the config files and add them to `routing_dev.yml`. Examples here https://symfony.com/doc/current/routing.html – JimL Nov 26 '17 at 17:36

2 Answers2

0

You could make use of a third party by Qandidate-labs called toggle. https://github.com/qandidate-labs/qandidate-toggle-bundle

The toggle can be set up based off an entry in your parameters.yml file Or I suspect you could do it based on the env

And then at the top of the method you just use the annotation similar to below:

/**
 * @Toggle("another-cool-feature")
 */
public function barAction()
{
}
Greg
  • 113
  • 1
  • 14
0

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
Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
  • Hello, what if I have my TestController inside my AppBundle? Do I need to move it? Thanks. – StockBreak Nov 27 '17 at 13:48
  • I don't know how 'complicated' your TestController is, but maybe you want to create a TestBundle which is added to the AppKernel in the dev/test environment only (see `AppKernel::registerBundles`). – Stephan Vierkant Nov 27 '17 at 14:23
  • 1
    I'll look if you can 'exclude' a particular Controller if you include a directory like my example. – Stephan Vierkant Nov 27 '17 at 14:23