簡體   English   中英

Symfony 2.8原則

[英]Symfony 2.8 doctrine

我有一個簡單的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;
}

但是,在我的抽象控制器中獲取教義會引發錯誤:

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

我如何在我的抽象課中獲得學說?

為了使用$this->get(something.or.other) ,您的容器需要擴展ContainerAware ,如果您僅extends Controller ,它將自動執行。

作為2.8, ContainerAware被棄用,取而代之的ContainerAwareTrait ,但如果你的抽象控制器僅僅簡單地擴展控制器,那么你就不用擔心自己這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM