简体   繁体   English

如何在自定义主文件中访问Symfony组件

[英]How to access Symfony Components in a custom main file

I am working to make a batch file which will get some data from DB and send email. 我正在制作一个批处理文件,它将从数据库中获取一些数据并发送电子邮件。 A folder Batch is created in the same level of app and web . app and web的同一级别创建一个文件夹Batch Now my question is - how to access DB and other controls out of controller. 现在我的问题是-如何从控制器之外访问数据库和其他控件。

I tried to import use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Route; use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Template; 我尝试导入use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Route; use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Template; use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Route; use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Template; but it gives error saying no such files found. 但给出错误消息,提示未找到此类文件。

I don;t want it to be command/console app. 我不希望它成为命令/控制台应用程序。

My aim is to create a file which uses symfony components and Doctrine by staying outside bundle/controller. 我的目标是通过不使用捆绑软件/控制器来创建使用symfony组件和教义的文件。 How do I import files in the file?. 如何导入文件中的文件?

First I really recommend to create a Symfony Console Command for this. 首先,我真的建议为此创建一个Symfony控制台命令

But if you really want to create a file (lets say test.php ) in your document root, you can load a symfony service like this: 但是,如果您真的想在文档根目录中创建一个文件(比如说test.php ),则可以加载一个symfony服务,如下所示:

<?php
require __DIR__.'/app/autoload.php'; // load the autoloader

$kernel = new AppKernel('prod', false); // create a kernel instance
$kernel->loadClassCache(); // load the class cache
$kernel->boot(); // boot the kernel which will initialise the container.

$myInstance = $kernel->getContainer()->get('myservice');

$myInstance->doWhatever();

PS: In there you should also be able to make use of all other symfony components. PS:在那里,您还应该可以使用所有其他symfony组件。

PPS: The Notice: Use of undefined constant OCI_COMMIT_ON_SUCCESS - assumed 'OCI_COMM IT_ON_SUCCESS' error tells you that something is wrong with your oci8 extension try to reinstall/install or enable it again. PPS: Notice: Use of undefined constant OCI_COMMIT_ON_SUCCESS - assumed 'OCI_COMM IT_ON_SUCCESS'错误,告诉您oci8扩展程序出现问题,请尝试重新安装/安装或再次启用它。 http://php.net/manual/en/oci8.installation.php http://php.net/manual/zh/oci8.installation.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Symfony组件配置对自定义验证器的依赖关系? - How to configure dependencies on custom validator with Symfony Components? 在主应用程序中包括Symfony2独立控制台组件软件包,以访问命令 - Including Symfony2 standalone console components packages in main app to access commands 如何在Symfony中的自定义存储库中访问验证器服务 - How to access validator service in custom repository in Symfony 如何访问symfony2中的自定义类? - how to access custom classes in symfony2? 如何在不声明任何变量的情况下访问symfony会话? (Symfony组件与旧版代码用例进行交互) - How to access a symfony session without declaring any variables? (Symfony components interacting with Legacy code use case) 如何使用Symfony2访问路由文件上的数据库? - How access to database on routing file with Symfony2? 具有Symfony组件的自定义框架的路由系统 - Routing System for Custom Framework with Symfony Components 我如何在symfony2中拥有自定义访问被拒绝页面 - How can i have custom access denied page in symfony2 Symfony | 如何从控制器中的存储库访问自定义方法? - Symfony | How to access the custom methods from the respository in a controller? Symfony2 - 如何在自定义控制台命令中访问服务? - Symfony2 - How to access the service in a custom console command?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM