我有一个连接到Elasticsearch的PHP文件,在这里我对文档进行索引。
我的elasticIndex.php文件:
**class elasticIndex{
function elasticFun(){
require 'vendor/autoload.php';
$client = new Elasticsearch\Client();
$feed = 'http://blaasd.zasdp.tv/Xml';
$xml = simplexml_load_file($feed);
foreach ($xml-> .......
......}**
现在的问题是我正在使用Symfony框架,在该框架中,必须在PHP文件的开头寻址 namespace ,例如:
**namespace MyBundle\Controller**
这就是为什么我无法在 Controller 类中使用以下两行的原因:
require 'vendor/autoload.php';
$client = new Elasticsearch\Client();
因此,我创建了一个新的PHP文件(elasticIndex.php),并编写了用于Elasticsearch索引编制的代码。因此,如何从我的elasticIndex类到 Controller 类调用函数
elasticFun()
。因为没有在elasticIndex类中使用命名空间,所以它不在我的Symfony项目中的任何地方。那么,如何调用我的
elasticFun()
函数,该函数未从 Controller 类寻址到任何地方?我也尝试使用像---这样的全局 namespace
**use MyBundle\VideoProviderClient\elasticIndex as _val;
class ExternalClientControlController extends Controller {
public function ExternalClientControlAction() {
$_val = new \elasticIndex();
return new Response($_val);
}**
它给出了一个错误-试图从全局命名空间中加载类“elasticIndex”。您是否忘记了“use”语句?
谁能帮我这个忙。非常感谢高级...
最佳答案
您需要将ElasticSearch对象的 namespace 导入到 Controller 类中。
通常,这可以通过在文件顶部附近(类的 namespace 声明下)的use语句来完成,即:
namespace MyBundle\Controller
use Elasticsearch;
class index {
public function indexAction() {
$client = new Elasticsearch\Client();
}
}
对于任意PHP文件,您可以将它们重构为Symfony / Composer的自动加载结构,也可以使用ClassLoader功能手动加载最适合您特定要求的类:
关于php - 使用一个PHP文件来Symfony,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30819562/