简体   繁体   English

Symfony 2.8原则

[英]Symfony 2.8 doctrine

I have an simple Abstract ApiController (extends from Controller) class, where i handle Auth. 我有一个简单的Abstract ApiController(从Controller扩展)类,在其中处理Auth。

protected function handleAuth(){
    $login = $this->request->getUser();
    $password = $this->request->getPassword();

    $user = $this->get('doctrine.orm.entity_manager');

    if (!$user)
    {
        return $this->sendResponse(array('msg'=>'User not found'),404);
    }
    $user = $user[0];

    if(!password_verify($data['password'], $user->getUserPass())){
        return $this->sendResponse(array('msg'=>'Invalid credentials'),401);
    }

    return true;
}

But it throws an erorr with getting doctrine in my Abstract Controller: 但是,在我的抽象控制器中获取教义会引发错误:

Error: Call to a member function get() on a non-object
in vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php at line 391

How can i get doctrine in my Abstract class? 我如何在我的抽象课中获得学说?

In order to use $this->get(something.or.other) , your container needs to extend ContainerAware , which it will automatically do if you simply extends Controller . 为了使用$this->get(something.or.other) ,您的容器需要扩展ContainerAware ,如果您仅extends Controller ,它将自动执行。

As of 2.8, ContainerAware is deprecated in favor of ContainerAwareTrait , but if your abstract controller simply extends Controller, then you don't need to worry yourself with that. 作为2.8, ContainerAware被弃用,取而代之的ContainerAwareTrait ,但如果你的抽象控制器仅仅简单地扩展控制器,那么你就不用担心自己这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM