質問

I have implemented Zend Auth by creating a getServiceConfig()-like AuthenticationService Object in Module.php:

'AuthService' => function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'tblUSRUsers', 'Email', 'Password', "MD5(?)");

                    $authService = new AuthenticationService();
                    $authService->setAdapter($dbTableAuthAdapter);
                    $authService->setStorage($sm->get('Application\Model\MyAuthStorage'));

                    return $authService;
                },

In the controller action, I am getting this object by

$this->getServiceLocator()
                    ->get('AuthService');

For authentication I obtain the values using

$authservice    = $this->getServiceLocator()->get('AuthService');
$arrUserDetails = $authservice->getIdentity();

Its working fine, these values are available.

But the problem is that ServiceLocator is not available in the Controller constructor so can't write the above code there. Writing this code in each and every action doesn't seem to be a good practice. Could anyone help out with this?

役に立ちましたか?

解決

Possible solution would be to have a route event handler, where you set user credentials:

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager = $e->getApplication()
            ->getEventManager ();

        $eventManager->attach (
            MvcEvent::EVENT_ROUTE,
            function  (MvcEvent $e)
            {
                $auth = $e->getApplication()
                          ->getServiceManager()
                          ->get('AuthService');

                $e->setParam('userinfo', $auth->getUserInfo());
                // update 
                $layout = $e->getViewModel();
                $layout->userinfo = $auth->getUserInfo();           
       });
    }

and then access then in the controller like this:

class IndexController extends AbstractActionController
{
    public function indexAction ()
    {
        $this->getEvent()->getParam('userinfo');

他のヒント

There are a couple of ways to do it. You can overload the ControllerConfig method in the Module class so you can instantiate the controllers however you like. Or you can write a controller plugin that will handle the authentication & return the identity.

EDIT

Should have provided these links earlier, but didn't have much access from the phone ;).

To understand ControllerConfig take a look at this blog post. There's an explanation about half way down of the getControllerConfig method in the Module class. This is a method the Module Manager looks for when instantiating the Module classes which allows you to customize the instantiation of a Controller in that Module. Once you return the Controller class, the Controller Manager then injects the rest of the Controller dependencies (plugins, etc.)

Controller plugins are small classes that get injected to every controller and which can be invoked from an action to do some common bit of code. When you use $this->getServiceLocator() in a controller action, you're using a plugin.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top