JMSDiExtraBundle
JMSDiExtraBundle copied to clipboard
@Inject does't work in non-controller class.
Here is a Service:
// src/Irjournal/MergeBundle/Libs/Sugar/sugarDefaults.php
namespace Irjournal\MergeBundle\Libs\Sugar;
use JMS\DiExtraBundle\Annotation\Service;
/**
* @Service("sugar.defaults", public=true)
*/
class sugarDefaults {
function getUrl(){
return $this->getDefaults('url');
}
function test() {
return "DATA From service".__CLASS__;
}
}
Here is Injection in SymfonyController:
// src/Irjournal/MergeBundle/Controller/dbSearchAjaxServer.php
namespace Irjournal\MergeBundle\Controller;
use Irjournal\MergeBundle\Libs\Sugar\sugarAjaxRequest;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use JMS\DiExtraBundle\Annotation as DI;
class dbSearchAjaxServer extends Controller {
/**
* @DI\Inject("sugar.defaults")
*/
public $sd;
/**
* @Route("/dbsearch/ajaxcustom", name="dbsearchajaxcustom")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
function dbsearchAllFieldsAction(Request $request) {
$response = new Response();
$sar=new sugarAjaxRequest($request);
echo print_r($this->sd,true)."||||";
$d=$sar->getDataByModule('Contacts');
die($d);
return $response;
}
}
and echo work fine. Browser shows print_r(of_service_obj) But the same rows in non-controller class (bellow),show me error...
// src/Irjournal/MergeBundle/Libs/Sugar/sugarAjaxRequest.php
namespace Irjournal\MergeBundle\Libs\Sugar;
use JMS\DiExtraBundle\Annotation\Service;
use Irjournal\MergeBundle\IrjournalMergeBundle;
use Symfony\Component\HttpFoundation\Request;
use Irjournal\MergeBundle\Libs\Sugar\moduleFields;
use JMS\DiExtraBundle\Annotation\Inject;
/**
* @author mopkob
*
*/
class sugarAjaxRequest {
/**
* @Inject("sugar.defaults")
*/
public $defaults;
protected $prefix;
protected $focused;
protected $orig=array();
function __construct(Request $req) {
$fields=$req->get('fields');
$this->focused=$this->cutPrefix($req->get('focused'));
foreach ($fields as $key => $val) {
$this->orig[$this->cutPrefix($key)]=$val;
}
}
function getModules() {
return $modules;
}
function getDataByModule($module) {
die(print_r($this->defaults->test()));
}
function cutPrefix($key) {
$tmp=explode('_', $key);
if (count($tmp)>1)array_shift($tmp);
return implode('_', $tmp);
}
}
Here is error:
Irjournal\MergeBundle\Libs\Sugar\sugarDefaults Object ( ) ||||
Whoops, looks like something went wrong.
1/1FatalErrorException: Error: Call to a member function test() on a non-object in /var/www/dev/tbs/src/Irjournal/MergeBundle/Libs/Sugar/sugarAjaxRequest.php line 61
in /var/www/dev/tbs/src/Irjournal/MergeBundle/Libs/Sugar/sugarAjaxRequest.php line 61
at ErrorHandler??handleFatal() in /var/www/dev/tbs/vendor/symfony/symfony/src/Symfony/Component/Debug/ErrorHandler.php line 0
at sugarAjaxRequest??getDataByModule() in /var/www/dev/tbs/src/Irjournal/MergeBundle/Controller/dbSearchAjaxServer.php line 37
at dbSearchAjaxServer??dbsearchAllFieldsAction() in /var/www/dev/tbs/app/bootstrap.php.cache line 0
at ??call_user_func_array() in /var/www/dev/tbs/app/bootstrap.php.cache line 2844
at HttpKernel??handleRaw() in /var/www/dev/tbs/app/bootstrap.php.cache line 2818
at HttpKernel??handle() in /var/www/dev/tbs/app/bootstrap.php.cache line 2947
at ContainerAwareHttpKernel??handle() in /var/www/dev/tbs/app/bootstrap.php.cache line 2249
at Kernel??handle() in /var/www/dev/tbs/web/app_dev.php line 28
at ??{main}() in /var/www/dev/tbs/web/app_dev.php line 0
I think that the same situation like here: http://stackoverflow.com/questions/18533881/jmsdiextrabundle-inject-does-not-work-in-custom-classes-but-works-in-the-contr/18534037#18534037
Please, investigate this bug and let me know about this.
Thank you.
@Inject is about configuring the service in the DI container (like everything else in DIExtraBundle).
$sar=new sugarAjaxRequest($request); has no way to be instrumented by JMSDIExtraBundle.
Dear Stof,
Thank you for you comment. Could you please explain me what i have to do with my class to use annotation with JMSDIExtraBundle.