问题描述
当产品环境处于活动状态时,是否可以禁用特定操作?
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:检查控制器中的环境
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
}
选项2:仅将路由包含在开发环境中
对于此选项,让我重新说明一下您的问题:如何在中启用控制器>发展环境? (而不是在生产中禁用)
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)
看看,可以用作新应用程序框架的全功能Symfony应用程序。。它具有一个开发环境,其中包括WebProfilerBundle(又称Web调试工具栏)的路由。
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).
在您的 dev
环境中, 已加载。您可以定义扩展主路由器的路由文件:
In your dev
environment, config_dev.yml is being loaded. You can define a routing file that extends the main router:
framework:
router:
resource: '%kernel.project_dir%/app/config/routing_dev.yml'
strict_requirements: true
profiler: { only_exceptions: false }
test:
resource: '@TestBundle/Controller/'
type: annotation
_main:
resource: routing.yml
这篇关于Symfony在生产中禁用控制器动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!