使用Symfony和Doctrine在null 500上调用成员函数has() [英] Call to a member function has() on null 500 with Symfony and Doctrine

查看:26
本文介绍了使用Symfony和Doctrine在null 500上调用成员函数has()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在以下服务文件中设置要运行的学说.

I am having some trouble setting up doctrine to run from the following service file.

<?php

// src/AppBundle/Services/SuperusersService.php
namespace AppBundle\Services;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Doctrine\ORM\EntityManager;

class SuperusersService extends Controller
{

    public function sayHello()
    {

        $em = $this->getDoctrine()->getManager();

         // Query here

    }

}

运行上述命令时,出现以下错误消息.

When I run the above I get the following error message.

在null上调用成员函数has()500内部服务器错误-FatalThrowableError

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

我从以下控制器文件中调用该文件.

I call the file from the following controller file.

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class LoginController extends Controller
{
    /**
     * @Route("/login")
     */
    public function indexAction(Request $request)
    {

        if(!empty($_POST)){

            ////
            // If for submitted attempt to log the user in.
            ////

            $superusersService = $this->get('app.superusers.services');

            $superusersService->sayHello();

        }

        $name = "Login";

        return $this->render('login/login.html.php', 
        array());

    }
}

服务文件

# Learn more about services, parameters and containers at
# http://symfony.com/doc/current/service_container.html
parameters:
#    parameter_name: value

services:
#    service_name:
#        class: AppBundle\Directory\ClassName
#        arguments: ["@another_service_name", "plain_value", "%parameter_name%"]
    app.superusers.services:
            class: AppBundle\Services\SuperusersService

现在,我环顾了其他问题,但到目前为止,我仍无法解决以上问题.我认为这与文件的设置有关,但是我不确定是什么.

Now I have taken a look around at other questions but as of yet I am unable to resolve the above. I think it will have something to do with the setup of the files but I'm not sure what.

推荐答案

将控制器声明为服务时,容器属性不再存在,因此您将无法再使用

When a controller is declared as a service the container attribute is no longer present hence you can no longer get a service with

$this->get('service_name')

解决方案:您需要像常规服务那样注入依赖项,这是一个

The solution: You need to inject your dependencies the way you would for a regular service, here is an example

另一方面,我不确定这是否只是测试代码,但您可能应该避免使用$ _POST全局变量,可以使用提供的$ request方法.

On a side note, i'm not sure if this is just testing code but you should probably avoid using $_POST globals, you can make use of the $request method provided.

这篇关于使用Symfony和Doctrine在null 500上调用成员函数has()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆