问题
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;
}
}
回答1:
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()
{
}
回答2:
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
来源:https://stackoverflow.com/questions/47439037/symfony-disable-controller-actions-in-production