我在设置要从以下服务文件运行的学说时遇到一些问题。
<?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
}
}
当我运行上述命令时,我收到以下错误消息。
在 null 500 内部服务器错误上调用成员函数 has() -
致命可抛出错误
我从以下控制器文件中调用该文件。
<?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
现在我已经查看了其他问题,但到目前为止我还无法解决上述问题。我认为这与文件的设置有关,但我不确定是什么。
当控制器被声明为服务时,容器属性不再存在,因此您无法再获取服务
$this->get('service_name')
解决方案:您需要像常规服务一样注入依赖项,这是一个example https://symfony.com/doc/current/controller/service.html#alternatives-to-base-controller-methods
顺便说一句,我不确定这是否只是测试代码,但您可能应该避免使用 $_POST 全局变量,您可以使用提供的 $request 方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)