在 symfony2 中使用 elasticsearch
Use elasticsearch in symfony2
我已经使用 composer 安装了 elasticsearch。这是我的 AppKernel.php 文件
new Elasticsearch\Client()
这是我的 TestController.php 文件。
<?php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Elasticsearch\Client;
//use Elasticsearch\Common\AbstractFactory;
class TestController extends Controller{
/**
* @Route("/", name="test-homepage")
*/
public function indexAction(){
$client = new Elasticsearch\Client();
dump($client);
die;
}
}
我正在使用 eclipse 作为我的 ide,它向我显示 elasticsearch\Client cannot be resolved.Why 这样的错误,这不起作用吗?
首先:如果你定义了use
语句那么你就不需要FQCN。顺便说一句,您的 FQCN 不正确,它应该以 \
开头,以防止从当前命名空间加载 class。
然后:在AppKernel.php
中你需要定义包,而不是你安装的每个库。
如果你在PHP方面经验不足,请尝试使用更易于学习的框架。 Symfony 主要面向经验丰富的开发人员。
如果您认为可以使用 Symfony,那么我建议您使用捆绑包来集成 ElasticSearch 和 Elastica:https://github.com/FriendsOfSymfony/FOSElasticaBundle。它将节省您的时间。
您应该尝试实例化导入的 class,使用短名称(基于 class 上的 use 语句),如下所示:
$client = new Client();
或使用 FQCN:
$client = new \Elasticsearch\Client();
(请注意,新语句以 \ 开头,这会阻止尝试从当前命名空间加载 class(即:AppBundle\Controller : \AppBundle\Controller\Elasticsearch\Client )
我已经使用 composer 安装了 elasticsearch。这是我的 AppKernel.php 文件
new Elasticsearch\Client()
这是我的 TestController.php 文件。
<?php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Elasticsearch\Client;
//use Elasticsearch\Common\AbstractFactory;
class TestController extends Controller{
/**
* @Route("/", name="test-homepage")
*/
public function indexAction(){
$client = new Elasticsearch\Client();
dump($client);
die;
}
}
我正在使用 eclipse 作为我的 ide,它向我显示 elasticsearch\Client cannot be resolved.Why 这样的错误,这不起作用吗?
首先:如果你定义了use
语句那么你就不需要FQCN。顺便说一句,您的 FQCN 不正确,它应该以 \
开头,以防止从当前命名空间加载 class。
然后:在AppKernel.php
中你需要定义包,而不是你安装的每个库。
如果你在PHP方面经验不足,请尝试使用更易于学习的框架。 Symfony 主要面向经验丰富的开发人员。
如果您认为可以使用 Symfony,那么我建议您使用捆绑包来集成 ElasticSearch 和 Elastica:https://github.com/FriendsOfSymfony/FOSElasticaBundle。它将节省您的时间。
您应该尝试实例化导入的 class,使用短名称(基于 class 上的 use 语句),如下所示:
$client = new Client();
或使用 FQCN:
$client = new \Elasticsearch\Client();
(请注意,新语句以 \ 开头,这会阻止尝试从当前命名空间加载 class(即:AppBundle\Controller : \AppBundle\Controller\Elasticsearch\Client )