0

In Symfony 2 I am using this bundle library (https://github.com/LeaseWeb/LswApiCallerBundle) to make API REQUEST.

This is the function to do it:

$this->get('api_caller')->call(new HttpPostJson($path, $object));

If I put the above function in the DefaultController it works. But I would like to use that function in my external class without extend controller.

Thanks

1 Answer 1

1

This is the moment you would register a new service in the service container.

In your bundle's Resources/config/services.yml, assuming you're using YAML

services:
    your_service:
        class:     Your\Bundle\Namespace\YourClassName
        arguments: ["@api_caller"]

Then in your external class

<?php

namespace Your\Bundle\Namespace;

use Lsw\ApiCallerBundle\Caller\LoggingApiCaller;
use Lsw\ApiCallerBundle\Call\HttpPostJson;

class YourClassName
{
    private $apiCaller;

    public function __construct(LoggingApiCaller $apiCaller)
    {
        $this->apiCaller = $apiCaller;
    }

    public function doSomething()
    {
        $this->apiCaller->call(new HttpPostJson($path, $object));

        //....
    }
}

then in your controller

class DefaultController extends Controller
{
    public function someAction()
    {
        $foo = $this->get('your_service');
        $foo->doSomething();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.