0

I have 5 class Categoria, Produto, Subcategoria, Subproduto and Comanda and for execute search in all classes i try make a service like:

 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

 class ComandaController extends Controller { ... public function newAction(Request $request, $id) { $comanda = new Comanda(); $categorias = $this->get('categorias.ativas')->CategoriasAtivasAction(); ... 

Then symfony return error

Call to a member function has() on null 500 Internal Server Error - FatalThrowableError

My services.yml has

 services: categorias.ativas: class: AppBundle\Controller\AccessClassController 

Whats wrong?

2
  • 1
    You can't use a Controller (extending Controller) as a service. Commented Aug 3, 2016 at 21:14
  • Actually you can as long as you inject the container. Doesn't make much sense to do so. And in this case, it is the ComandaController which is bbeing created somewhere without the container. Hence the has error. Commented Aug 3, 2016 at 21:24

1 Answer 1

2

As per Symfony documentation, Defining controllers as services is not officially recommended by 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: categorias.ativas: class: AppBundle\Controller\AccessClassController arguments: ["@service_container"] 

And in your AccessClassController file

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; } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.