尝试创建新路由时{Zend \ ServiceManager \ Exception \ ServiceNotCreatedException

时间:2017-01-10 15:12:21

标签: php zend-framework routes

我尝试在我的一个模块中创建一个新路由,以对应我项目中的新Controller。

但我收到了这个错误,我不知道它来自哪里:

  

在尝试创建shopmanagementcontrollershopmanagement(别名:Shopmanagement \ Controller \ Shopmanagement)时,为此实例类型注册了无效工厂。

我得到了这个Stacktrace:

  

0 /var/www/routedudrive.fr/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(634):   的Zend \的ServiceManager \ AbstractPluginManager-> createFromFactory(' shopmanagementc ...&#39 ;,   ' Shopmanagement \ ...&#39)

     

1 /var/www/routedudrive.fr/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(597):   的Zend \的ServiceManager \ ServiceManager-> doCreate(' Shopmanagement \ ...&#39 ;,   ' shopmanagementc ...&#39)

     

2 /var/www/routedudrive.fr/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(530):   的Zend \的ServiceManager \ ServiceManager->创建(阵列)

     

3 /var/www/routedudrive.fr/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php(161):   Zend \ ServiceManager \ ServiceManager-> get(' Shopmanagement \ ...',true)

     

4 /var/www/routedudrive.fr/vendor/zendframework/zend-mvc/src/DispatchListener.php(94):   的Zend \的ServiceManager \ AbstractPluginManager->获得(' Shopmanagement \ ...&#39)

     

5 [内部函数]:Zend \ Mvc \ DispatchListener-> onDispatch(Object(Zend \ Mvc \ MvcEvent))   6 /var/www/routedudrive.fr/vendor/zendframework/zend-eventmanager/src/EventManager.php(490):   call_user_func(Array,Object(Zend \ Mvc \ MvcEvent))

     

7 /var/www/routedudrive.fr/vendor/zendframework/zend-eventmanager/src/EventManager.php(263):   的Zend \ eventmanager进行\ EventManager-> triggerListeners('调度&#39 ;,   Object(Zend \ Mvc \ MvcEvent),Object(Closure))

     

8 /var/www/routedudrive.fr/vendor/zendframework/zend-mvc/src/Application.php(340):   的Zend \ eventmanager进行\ EventManager-> triggerEventUntil(对象(闭合),   对象(的Zend \的mvc \ MvcEvent))

     

9 /var/www/routedudrive.fr/public/index.php(21):Zend \ Mvc \ Application-> run()

     

10 {main}

这是我的 module.config.php

<?php
return array(
    'controllers' => array(
        'factories' => array(
            'Backshop\Controller\Backshop' => Backshop\ControllerFactory\BackshopControllerFactory::class,
            'Shopmanagement\Controller\Shopmanagement' => Shopmanagement\ControllerFactory\ShopmanagementControllerFactory::class,
        ),
    ),
    'router' => array(
        'routes' => array(
            'backshop' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/backshop[/:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9a-zA-Z]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Backshop\Controller\Backshop',
                        'action'     => 'index',
                    ),
                ),
            ),
            'shopmanagement' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/shopmanagement[/:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9a-zA-Z]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Shopmanagement\Controller\Shopmanagement',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),

    'view_manager' => array(
        'template_path_stack' => array(
            'backshop' => __DIR__ . '/../view',
            'shopmanagement' => __DIR__ . '/../view',
        ),
    ),
);

这是我的 ShopmanagementControllerFactory.php

<?php
namespace Shopmanagement\ControllerFactory;
use Shopmanagement\Controller\ShopmanagementController;
use \Zend\ServiceManager\FactoryInterface;
use \Zend\ServiceManager\ServiceLocatorInterface;

class ShopmanagementControllerFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator) {
        $sm  = $serviceLocator->getServiceLocator();
        //$merchantTable = $sm->get('Backshop\Model\MerchantTable');
        //$accountValidationTable = $sm->get('Backshop\Model\AccountValidationTable');
        $controller = new ShopmanagementController();
        return $controller;
    }
}

这是我的 ShopmanagementController.php

<?php
namespace Shopmanagement\Controller;

use Zend\View\Model\ViewModel;

class ShopmanagementController extends AbstractActionController{

    public function __construct()
    {

    }

    public function indexAction(){
        return new ViewModel();
    }
}

这是我的文件树:

Filetree

如果你知道问题出在哪里,所有帮助都会表示赞赏:)

提前谢谢你,

托马斯

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案,如果它可以帮助某人。

我忘了在Module.php文件中引用我的命名空间,如下所示:

public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                    'Shopmanagement'        => __DIR__ . '/src/' . 'Shopmanagement', //This line
                ),
            ),
        );
    }