1

I'm trying to get https://github.com/zfcampus/zf-oauth2 working with my Application (mainly because I have installed apigility and zf-oauth2 comes with it).

I'm reading the very last section and it says to protect, I just simply use the following code (for instance, at the top of a controller):

if (!$this->server->verifyResourceRequest(OAuth2Request::createFromGlobals())) {
    // Not authorized return 401 error
    $this->getResponse()->setStatusCode(401);
    return;
}
// where $this->server is an instance of OAuth2\Server (see the AuthController.php).

However, $this->server has to be injected somehow. But, I can't seem to find how and what to inject. By clicking on the link to see AuthController.php, I get a page out found...

Edit: Thanks to Tom and Ujjwal, I think I am one step closer.

In my controller, now I have the following:

use ZF\OAuth2\Controller\AuthController;

class BaseController extends AuthController
{

}

In my Module.php, I try injecting OAuth2Server as such:

public function getServiceConfig()
{
    return array(
       'factories' => array(
            '\Stand\Controller\BaseController' =>  function ($sm) {
                $cls = new BaseController($sm->get('ZF\OAuth2\Service\OAuth2Server'));
                return $cls;
            },
        )
    );
}

But, when I tried to render the page, it is not catching my inject. I get

Catchable fatal error: Argument 1 passed to ZF\OAuth2\Controller\AuthController::__construct() must be an instance of OAuth2\Server

Please advice!

Thanks

juworld
  • 140
  • 1
  • 15
  • Here is the orginal link of [AuthController.php](https://github.com/zfcampus/zf-oauth2/blob/master/src/Controller/AuthController.php) – Ujjwal Ojha Jun 02 '14 at 14:26
  • The AuthControllerFactory injects the OAuth2Server service into the Auth Controller. See this link https://github.com/zfcampus/zf-oauth2/blob/master/src/Factory/AuthControllerFactory.php – Tom Metcalfe Jun 02 '14 at 16:57
  • Oh okay. Still new to Zf2. But, I'm guessing, I can just extend my controller using AuthController? And update the Module.php to inject the Oath2Server? I edited my original post to show what I have done. – juworld Jun 03 '14 at 02:24
  • Hi @juworld, i am having the same issue on how to inject OAuth2 server into my controller. Have you found a solution and do you mind to share? – max Jul 09 '15 at 15:00
  • Hi @max, sorry for not responding earlier. Are you still having the same problem? I believe I have a work around but I kind of forgot the detail (this was posted a year ago). Let me know if you still need help with this. – juworld Jul 31 '15 at 04:29

0 Answers0