Reputation: 153
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
Upvotes: 0
Views: 339
Reputation: 7808
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();
}
}
Upvotes: 1