Reputation: 847
I am working with a Symfony2 project.
I am trying to use a pice of data decleared in parameters.yml and use this data in one of my controllers.
I have read this in Symfony2 documentation about getParameters()
but it does not work.
In my parameters.yml i have done this:
sitemap_root_url: http:/example.co.uk/news/
In my controller I am trying this:
$this->test = $this->container->getParameters('sitemap_root_url');
This is the error I get:
Notice: Undefined property $container
Upvotes: 3
Views: 2240
Reputation: 1
for symfony 2.6
$this->container->getParameter('api_key');
above symfony 2.6
$this->getParameter('api_key');
Upvotes: 0
Reputation: 763
Try $this->container->getParameter('sitemap_root_url');
It works well for me, I use Symfony 2.5.
If you want to use container in your controller you can extend
Symfony\Bundle\FrameworkBundle\Controller\Controller (Symfony base controller).
// src/AppBundle/Controller/HelloController.php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HelloController extends Controller
{
// ...
}
Then you will be able to get your property. Example Controller which works for me:
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Response
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HelloController extends Controller
{
public function indexAction()
{
return new Response(
$this->container->getParameter('sitemap_root_url');
);
}
}
Upvotes: 2
Reputation: 653
Hey i have to answer (i wanted to edit the first answer) but you have an unnecessary s in your getParameters()
, that's all the problem i guess ;)
Upvotes: 1
Reputation: 1884
Apparently $container
is not declared in your controller, if you want to access the controller you can extend Symfony\Bundle\FrameworkBundle\Controller\Controller
and then use $this->container->getParameter('sitemap_root_url')
Otherwise you'll have to declare your controller as a service and inject the container.
Upvotes: 2