在自定义用户模块

时间:2015-10-11 22:28:44

标签: php dependency-injection zend-framework2 zfcuser

我已阅读Extending ZfcUser's UserController in custom module并完成了所有内容,但我仍然收到错误:

/vagrant/app/vendor/zf-commons/zfc-user/src/ZfcUser/Controller/UserController.php:69 您必须提供可调用的redirectCallback

Stack trace:
#0 /vagrant/app/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php(207): ZfcUser\Controller\UserController->__construct()
#1 /vagrant/app/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(642): Zend\ServiceManager\AbstractPluginManager->createFromInvokable('zfcuser', 'zfcuser')
#2 /vagrant/app/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(598): Zend\ServiceManager\ServiceManager->doCreate('zfcuser', 'zfcuser')
#3 /vagrant/app/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(530): Zend\ServiceManager\ServiceManager->create(Array)
#4 /vagrant/app/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php(116): Zend\ServiceManager\ServiceManager->get('zfcuser', false)
#5 /vagrant/app/vendor/zendframework/zend-mvc/src/Controller/ControllerManager.php(137): Zend\ServiceManager\AbstractPluginManager->get('zfcuser', Array, false)
#6 /vagrant/app/vendor/zendframework/zend-mvc/src/Controller/Plugin/Forward.php(121): Zend\Mvc\Controller\ControllerManager->get('zfcuser')
#7 /vagrant/app/vendor/socalnick/scn-social-auth/src/ScnSocialAuth/Controller/UserController.php(118): Zend\Mvc\Controller\Plugin\Forward->dispatch('zfcuser', Array)
#8 /vagrant/app/vendor/zendframework/zend-mvc/src/Controller/AbstractActionController.php(82): ScnSocialAuth\Controller\UserController->loginAction()
#9 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#10 /vagrant/app/vendor/zendframework/zend-eventmanager/src/EventManager.php(444): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#11 /vagrant/app/vendor/zendframework/zend-eventmanager/src/EventManager.php(205): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#12 /vagrant/app/vendor/zendframework/zend-mvc/src/Controller/AbstractController.php(118): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#13 /vagrant/app/vendor/zendframework/zend-mvc/src/DispatchListener.php(93): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#14 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#15 /vagrant/app/vendor/zendframework/zend-eventmanager/src/EventManager.php(444): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#16 /vagrant/app/vendor/zendframework/zend-eventmanager/src/EventManager.php(205): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#17 /vagrant/app/vendor/zendframework/zend-mvc/src/Application.php(314): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#18 /vagrant/app/public/index.php(21): Zend\Mvc\Application->run()
#19 {main}

在MyModule \ Module.php中,我确实喜欢这样:

public function getControllerConfig()
{
    return array(
        'factories' => array(
            'User\Controller\User' => function($controllerManager) {
                $serviceManager = $controllerManager->getServiceLocator();
                $redirectCallback = $serviceManager->get('zfcuser_redirect_callback');
                $controller = new \ZfcUser\Controller\UserController($redirectCallback);
                return $controller;
            },
        ),
    );
}

我的扩展用户控制器如下所示:

class UserController extends \ZfcUser\Controller\UserController {
    public function loginAction() {
        // customs actions 
}

我还包括具有自己的UserController的ScnSocialAuth模块。 为什么我想这样做?我正在编写SPA应用程序(Backbone + Marionette),我需要模态认证。也许我错过了什么,或者错误的电话 - 请帮忙解决。我尝试了不同的方法,但这将是最优化的。此外,答案对其他用户也很有用。感谢。

2 个答案:

答案 0 :(得分:2)

控制器有自己的配置:

public function getControllerConfig()
{
  return [
    'factories' => [
    // snip

您的错误消息显示跟踪中的createFromInvokable。确保您没有具有相同定位器名称的invokable。

此外,您的工厂正在返回ZfcUser控制器,而不是您的自定义控制器。

答案 1 :(得分:0)

为了在Module.php中扩展ZfcUser控制器必须是:

public function getControllerConfig()
{
    return array(
        'factories' => array(
            'User\Controller\User' => function($controllerManager) {
                $serviceManager = $controllerManager->getServiceLocator();
                $redirectCallback = $serviceManager->get('zfcuser_redirect_callback');
                $controller = new \User\Controller\UserController($redirectCallback);
                return $controller;
            },
        ),
    );
}

需要调用自定义用户类而不是zfcuser。另外我的错误是在我的module.config.php中是它:

'controllers' => array(
    'invokables' => array(
        'User\Controller\User' => 'User\Controller\UserController'
    ),
),

因此,我的工厂没有被召唤。