1

请帮我解决 ZendFramework 中的以下问题。

我是 ZF 和 PHP7 的新手。几天来,我无法在控制器中使用 Doctrine EntityManager。

我有:

我的控制器

namespace Sonun\Controller;

use Zend\Mvc\Controller\AbstractActionController,
    Doctrine\ORM\EntityManager;

class IndexController extends AbstractActionController
{
    protected $entityManager;

    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

工厂

    namespace Sonun\Controller;

use Sonun\Controller\IndexController,
    Zend\ServiceManager\FactoryInterface,
    Zend\ServiceManager\ServiceLocatorInterface;

class IndexControllerFactory implements FactoryInterface
{

    public function createService(ServiceLocatorInterface $sm)
    {
        $entityManager = $sm->get("Doctrine\ORM\EntityManager");
        return new IndexController($entityManager);
    }
}

模块.config.php

return [

    "controllers" => [
        "invokables" => [
            "Sonun\Controller\IndexController" => "Sonun\Controller\IndexController"
        ]
    ],

    "router" => [
        "routes" => [
            "sonun" => [
                "type" => "segment",
                "options" => [
                    "route" => "/sonun/[:action/][:id/]",
                    "constraints" => [
                        "action" => "[a-zA-Z0-9_-]*",
                        "id" => "[0-9]*"
                    ],
                    "defaults" => [
                        "controller" => "Sonun\Controller\IndexController",
                        "action" => "index"
                    ]
                ]
            ]
        ]
    ],

    "view_manager" => [
        "template_path_stack" => [
            __DIR__."/../view"
        ]
    ],

    "service_manager" => [
        "factories" => [
            "Sonun\Controller\IndexController" => "Sonun\Controller\IndexControllerFactory"
        ]
    ]
]

错误

     Fatal error: Uncaught TypeError: Argument 1 passed to ZendDeveloperTools\Exception\SerializableException::__construct() must be an instance of Exception, instance of TypeError given, called in C:\xampp\htdocs\sonun\vendor\zendframework\zend-developer-tools\src\Collector\ExceptionCollector.php on line 45 and defined in C:\xampp\htdocs\sonun\vendor\zendframework\zend-developer-tools\src\Exception\SerializableException.php:26 Stack trace: #0 C:\xampp\htdocs\sonun\vendor\zendframework\zend-developer-tools\src\Collector\ExceptionCollector.php(45): ZendDeveloperTools\Exception\SerializableException->__construct(Object(TypeError)) #1 C:\xampp\htdocs\sonun\vendor\zendframework\zend-developer-tools\src\Profiler.php(210): ZendDeveloperTools\Collector\ExceptionCollector->collect(Object(Zend\Mvc\MvcEvent)) #2 C:\xampp\htdocs\sonun\vendor\zendframework\zend-developer-tools\src\Listener\ProfilerListener.php(93): ZendDeveloperTools\Profiler->collect(Object(Zend\Mvc\MvcEvent)) #3 C:\xampp\htdocs\sonun\vendor\zendframework\zend-eventmanag in C:\xampp\htdocs\sonun\vendor\zendframework\zend-developer-tools\src\Exception\SerializableException.php on line 26
4

2 回答 2

1

我也是 ZF3 的新手(不是每个人吗?)但我会试一试。你的工厂类需要看起来像

namespace Application\Controller\Factory;

use Zend\ServiceManager\Factory\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Interop\Container\ContainerInterface;
use Application\Controller\IndexController;

class IndexControllerFactory implements FactoryInterface {

    public function __invoke(ContainerInterface $container, $requestedName, Array $options = null) {    
        $entityManager = $container->get('doctrine.entitymanager.orm_default');
        return new IndexController($entityManager);
    }

}

请注意,在 ZF3 中,您的工厂现在必须实现Zend\ServiceManager\Factory\FactoryInterface,即实现__invoke()而不是createService().

在你的module.config.php,你的 Controller 不是一个可调用的——它有一个硬依赖$entityManager,对吧?- 所以你需要取消它并用类似的东西替换

'controllers' => [
    'factories' => [
       Controller\IndexController::class => Controller\Factory\IndexControllerFactory::class,
    ],
],

祝你好运!

于 2016-10-14T16:03:02.253 回答
0

您看到的特定错误由 ZendDeveloperTools 发出,并在其 1.1.1 版本中修复。跑去composer update zendframework/zend-developer-tools拿。

然而,这不是问题的根源。该模块只是试图报告异常,在这种情况下是类型错误;你必须从那里继续调试。

最后,据我所知,DoctrineModule(和相关模块)与 ZF 的 v3 版本不兼容。在他们完成迁移之前,您可能需要切换到 v2。

于 2016-09-10T13:43:49.490 回答