[英]Call to a member function has() on null symfony2
I have 5 class Categoria, Produto, Subcategoria, Subproduto and Comanda and for execute search in all classes i try make a service like: 我有5类Categoria,Produto,Subcategoria,Subproduto和Comanda,为了在所有类中执行搜索,我尝试提供以下服务:
namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class AccessClassController extends Controller{ /** * Retorna todas as categorias ativas */ public function CategoriasAtivasAction() { $em = $this->getDoctrine()->getManager(); $categorias = $em->getRepository('AppBundle:Categoria')->findByAtivo(1); return $categorias; } }
And i try access the service on ComandaController 我尝试在ComandaController上访问服务
class ComandaController extends Controller { ... public function newAction(Request $request, $id) { $comanda = new Comanda(); $categorias = $this->get('categorias.ativas')->CategoriasAtivasAction(); ...
Then symfony return error 然后symfony返回错误
Call to a member function has() on null 500 Internal Server Error - FatalThrowableError
My services.yml has 我的services.yml有
services: categorias.ativas: class: AppBundle\Controller\AccessClassController
Whats wrong? 怎么了?
As per Symfony documentation, Defining controllers as services is not officially recommended by Symfony. 根据Symfony文档,Symfony并不正式建议将控制器定义为服务。 Even though if you need to use you can do that.
即使您需要使用也可以这样做。 As per your code you haven't passed service container object in service.
根据您的代码,您尚未在服务中传递服务容器对象。 Please do the following changes and try.
请进行以下更改并尝试。
In your services.yml file 在您的services.yml文件中
services:
categorias.ativas:
class: AppBundle\Controller\AccessClassController
arguments: ["@service_container"]
And in your AccessClassController file 并在您的AccessClassController文件中
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class AccessClassController extends Controller{
/**
* Retorna todas as categorias ativas
*/
public function CategoriasAtivasAction()
{
$em = $this->container->get('doctrine')->getManager();
$categorias = $em->getRepository('AppBundle:Categoria')->findByAtivo(1);
return $categorias;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.