Reputation: 173
In Symfony3.4, the following error occurred while supporting automatic wiring.
The following error occurs even if the container is removed, just changing the class.
I changed the controller to an abstract controller due to another error, so I want to use the abstract controller as much as possible.
Is there anything I have forgotten?
https://symfony.com/doc/3.4/service_container/3.3-di-changes.html
Error
Attempted to call an undefined method named "getParameter" of class "Symfony\Component\DependencyInjection\ServiceLocator".
Controller.php
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class DefaultController extends AbstractController
{
/**
* @param KeepRequestService $keepRequestService
* @return array
*/
private function getKeepRequestSummary(KeepRequestService $keepRequestService): array
{
$summary = array();
//Error line
foreach (array_keys($this->container->getParameter('keep_request_status')) as $status) {
$params = array('status' => $status);
$summary[$status] = $keepRequestService->countKeepRequestBySearchParams($params);
}
return $summary;
}
services.yml
_defaults:
autowire: true
autoconfigure: true
public: false
App\:
resource: '../../src/*'
exclude: '../../src/{Ahi/Sp/AdminBundle/Model/Entity, Ahi/Sp/AdminBundle/Model/Repository, Ahi/Sp/AdminBundle/Resources/public/uploadify, Ahi/Sp/AdminBundle/Ahi/Sp/PublicBundle/ }'
App\Ahi\Sp\AdminBundle\Controller\:
resource: '../../src/Ahi/Sp/AdminBundle/Controller'
public: true
tags: ['controller.service_arguments']
Upvotes: 0
Views: 2053
Reputation: 173
As Vyctorya advised, I modified the code as below and the code disappeared.
Controller.php
/**
* @var array
*/
private $keepRequestStatus;
public function __construct(array $keepRequestStatus)
{
$this->keepRequestStatus = $keepRequestStatus;
}
private function getKeepRequestSummary(KeepRequestService $keepRequestService)
{
$summary = array();
foreach (array_keys($this->keepRequestStatus) as $status) {
$params = array('status' => $status);
$summary[$status] = $keepRequestService->countKeepRequestBySearchParams($params);
}
return $summary;
}
services.yml
services:
_defaults:
autowire: true
autoconfigure: true
public: false
bind:
$keepRequestStatus: '%keep_request_status%'
Upvotes: 1