简体   繁体   English

在null symfony2上调用成员函数has()

[英]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.

相关问题 错误:在空[Symfony2]上调用成员函数getPendingTransaction() - Error: Call to a member function getPendingTransaction() on null [Symfony2] symfony和ratchet在null上调用成员函数get() - symfony and ratchet Call to a member function get() on null Symfony 2.7:在null上调用成员函数get() - Symfony 2.7: Call to a member function get() on null 在symfony4中的null上调用成员函数setDp() - Call to a member function setDp() on null in symfony4 Symfony-在null上调用成员函数setFunction() - Symfony - Call to a member function setFunction() on null Symfony 5:在 null 上调用成员函数 encodePassword() - Symfony 5 :Call to a member function encodePassword() on null Symfony 4服务“在null上调用成员函数” - Symfony 4 Service “Call to member function on null” 在symfony2中的非对象错误上调用成员函数setCode() - Call to a member function setCode() on a non-object error in symfony2 在Symfony2(Doctrine)中调用非对象的成员函数format() - Call to a member function format() on a non-object in Symfony2 (Doctrine) Symfony2-登录时在非对象上调用成员函数getPassword() - Symfony2 - Call to a member function getPassword() on a non-object on login
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM