Istiak Mahmood
Istiak Mahmood

Reputation: 2422

Use a PHP file to Symfony

I have a PHP file which is connected to elasticsearch, where I am indexing my documents.

My elasticIndex.php file:

    **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-> .......
    ......}**

Now the problem is I am working on Symfony framework, where in the beginning of a PHP file you have to address the namespace, like:

**namespace MyBundle\Controller**

That is why I am unable to use the following two lines in my controller class:

require 'vendor/autoload.php';
    $client = new Elasticsearch\Client();

So I have created a new PHP file (elasticIndex.php)and writen code for elasticsearch indexing. So how can I call my function elasticFun() from my elasticIndex class to my controller class. for not using the namespace in the elasticIndex class, it is not address anywhere in my Symfony project.
So how can I call my elasticFun() function which is not addressed to anywhere from my controller class?

i have also try to use the global namespace like ---

**use MyBundle\VideoProviderClient\elasticIndex as _val;
class ExternalClientControlController extends Controller {
    public function ExternalClientControlAction() {
                $_val = new \elasticIndex();
        return new Response($_val);
    }**

it gives an error -- Attempted to load class "elasticIndex" from the global namespace.Did you forget a "use" statement?

Can anyone kindly help me with this. Thanks a lot in advanced ...

Upvotes: 1

Views: 553

Answers (1)

Maltronic
Maltronic

Reputation: 1802

You need to import the namespace for the ElasticSearch object into your controller class.

Typically this would be done with a use statement near the top of the file (under the namespace declaration for the class), i.e.:

namespace MyBundle\Controller

use Elasticsearch;

class index {

    public function indexAction() {
        $client = new Elasticsearch\Client();
    }
}

For arbitrary PHP files you can either refactor them into Symfony / Composer's autoloading structure or use the ClassLoader functionality to manually load the classes however best suited for your specific requirements:

https://github.com/symfony/ClassLoader

Upvotes: 1

Related Questions