我在设置doctrine以从以下服务文件运行时遇到一些麻烦.
<?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
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行上面的内容时,我收到以下错误消息.
在null 500内部服务器错误 - FatalThrowableError上调用成员函数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());
}
}
Run Code Online (Sandbox Code Playgroud)
服务文件
# 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
Run Code Online (Sandbox Code Playgroud)
现在我已经看了看其他问题,但到目前为止我无法解决上述问题.我认为这将与文件的设置有关,但我不确定是什么.
当控制器声明为服务时,容器属性不再存在,因此您无法再获取服务
$this->get('service_name')
Run Code Online (Sandbox Code Playgroud)
解决方案: 您需要按照常规服务的方式注入依赖项,这是一个示例
另外,我不确定这只是测试代码,但你应该避免使用$ _POST全局变量,你可以使用提供的$ request方法.
| 归档时间: |
|
| 查看次数: |
5234 次 |
| 最近记录: |