Closed digitalray closed 5 years ago
The error seems clear to me. This is not a Sonata issue, but how you fetch the service in your application. Closing.
You mean instead of inject it in the action? Public/private?
I’ve followed documentation and used the example in the doc in my code. If you don’t mind me asking, what is the correct way to fetch it?
Injecting it into the constructor of your controller would be the best option. Docs are outdated.
Thanks @kunicmarko20. When I added it in constructor I got a different error. constructor
public $seoPage;
public function __construct()
{
$this->seoPage = $this->container->get('sonata.seo.page');
}
Error Call to a member function get() on null
Error:
Call to a member function get() on null
at src/Controller/DefaultController.php:18
at App\Controller\DefaultController->__construct()
(var/cache/dev/ContainerOZovzCR/getDefaultControllerService.php:13)
at require('/Users/schakarian/repos/dr-web/var/cache/dev/ContainerOZovzCR/getDefaultControllerService.php')
(var/cache/dev/ContainerOZovzCR/srcDevDebugProjectContainer.php:391)
at ContainerOZovzCR\srcDevDebugProjectContainer->load('getDefaultControllerService.php')
(vendor/symfony/dependency-injection/Container.php:240)
at Symfony\Component\DependencyInjection\Container->make('App\\Controller\\DefaultController', 1)
(vendor/symfony/dependency-injection/Container.php:222)
at Symfony\Component\DependencyInjection\Container->get('App\\Controller\\DefaultController')
(vendor/symfony/http-kernel/Controller/ContainerControllerResolver.php:51)
at Symfony\Component\HttpKernel\Controller\ContainerControllerResolver->instantiateController('App\\Controller\\DefaultController')
(vendor/symfony/framework-bundle/Controller/ControllerResolver.php:54)
at Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver->instantiateController('App\\Controller\\DefaultController')
(vendor/symfony/http-kernel/Controller/ControllerResolver.php:110)
at Symfony\Component\HttpKernel\Controller\ControllerResolver->createController('App\\Controller\\DefaultController::index')
(vendor/symfony/http-kernel/Controller/ContainerControllerResolver.php:42)
at Symfony\Component\HttpKernel\Controller\ContainerControllerResolver->createController('App\\Controller\\DefaultController::index')
(vendor/symfony/framework-bundle/Controller/ControllerResolver.php:46)
at Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver->createController('App\\Controller\\DefaultController::index')
(vendor/symfony/http-kernel/Controller/ControllerResolver.php:85)
at Symfony\Component\HttpKernel\Controller\ControllerResolver->getController(object(Request))
(vendor/symfony/http-kernel/Controller/TraceableControllerResolver.php:38)
at Symfony\Component\HttpKernel\Controller\TraceableControllerResolver->getController(object(Request))
(vendor/symfony/http-kernel/HttpKernel.php:132)
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
(vendor/symfony/http-kernel/HttpKernel.php:66)
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
(vendor/symfony/http-kernel/Kernel.php:188)
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
(public/index.php:37)
Tried another method of injection but no luck
use Sonata\SeoBundle\SonataSeoBundle;
class DefaultController extends AbstractController
{
/**
* @Route("/default", name="default")
*/
public $seo;
public function __construct(SonataSeoBundle $seo)
{
$this->seo = $seo;
}
Error: Cannot autowire service "App\Controller\DefaultController": argument "$seo" of method "__construct()" references class "Sonata\SeoBundle\SonataSeoBundle" but no such service exists.
You shouldn’t not inject the Bundle, but the service instead: Try SeoPage from Sonata\SeoBubdle\Seo namespace
And IMO you should do:
Public function index(SeoPage $seo) ... instead of the constructor
Sorry SeoPageInterface should do the trick
Thanks alot @OskarStark it worked. Apologies for that mistake. It actually autocompleted but I should have caught it. The only other addition that I needed to do to make this work was to add an entry in the config/services.yaml file:
Sonata\SeoBundle\Seo\SeoPageInterface:
alias: "sonata.seo.page.default"
public: true
Here is the Controller
class DefaultController extends AbstractController
{
/**
* @Route("/default", name="default")
*/
public $seo;
public function __construct(Seo\SeoPageInterface $seo)
{
$this->seo = $seo;
}
public function index()
{
$this->seo->setTitle('test');
return $this->render('default/index.html.twig', [
'controller_name' => 'DefaultController',
]);
}
}
Great, but why public
?
IMO this should work, too:
Sonata\SeoBundle\Seo\SeoPageInterface:
alias: "sonata.seo.page.default"
the public
is only needed to get it via $this->get('....')
class DefaultController extends AbstractController
{
public function index(Seo\SeoPageInterface $seo)
{
$seo->setTitle('test');
return $this->render('default/index.html.twig', [
'controller_name' => 'DefaultController',
]);
}
}
Thanks again @OskarStark! Removing the public: true was OK. Also I think worked out for the best by putting it in the function instead of the constructor.
Environment
dev macOS Mojave v10.14.1
Sonata packages
Symfony packages
PHP version
Subject
When configuring sonata.seo.page with latest symfony project, I get a service not found error.
Steps to reproduce
Create sonata_seo.yaml in demo/config/packages
Create base template: demo/templates/base.html.twig
Create template: demo/templates/default/index.html.twig
Create a function in demo/src/DefaultController.php like this
Add route to demo/config/routes.yaml
Expected results
Page should render with title set to "some title"
Actual results
Service "sonata.seo.page" not found: even though it exists in the app's container, the container inside "App\Controller\DefaultController" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer", "session" and "twig" services. Unless you need extra laziness, try using dependency injection instead. Otherwise, you need to declare it using "DefaultController::getSubscribedServices()".
Stack Trace