【问题标题】:Zend 3 Argument 1 passed to Application\Controller\IndexController::__construct() must be an instance of , none given,Zend 3 传递给 Application\Controller\IndexController::__construct() 的参数 1 必须是 的实例,没有给出,
【发布时间】:2018-08-14 04:58:26
【问题描述】:

此配置适用于我的本地安装,但不适用于远程站点。

ExampleManager.php

<?php
namespace Application\Service;

use Application\Entity\SomeTable;

class ExampleManager 
{

    /**
     * Entity manager.
     * @var Doctrine\ORM\EntityManager
     */
    private $entityManager;

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

ExampleManagerFactory.php

<?php

namespace Application\Service\Factory;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use Application\Service\ExampleManager;

class ExampleManagerFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, 
    $requestedName, array $options = null)
    {
        $entityManager = $container->get('doctrine.entitymanager.orm_default');

        // instantiate the service and inject dependencies
        return new ExampleManager($entityManager);
    }
}

IndexControllerFactory.php

<?php

namespace Application\Controller\Factory;

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

class IndexControllerFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $exampleManager = $container->get(\Application\Service\ExampleManager::class);

        // instantiate the controller and inject dependencies
        return new IndexController($exampleManager);
    }
}

IndexController.php

<?php
/**
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Service\ExampleManager;

class IndexController extends AbstractActionController
{
    /**
     * Example manager.
     * @var Application\Service\ExampleManager
     */
    private $exampleManager;

    public function __construct(ExampleManager $exampleManager) 
    {
      $this->exampleManager = $exampleManager;
    }

module.config.php

<?php
/**
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Application;

use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Application\Service\ExampleManager;

return [
    'router' => [
        'routes' => [
            'home' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'application' => [
                'type'    => Segment::class,
                'options' => [
                    'route'    => '/application[/:action]',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
        ],
    ],
    'controllers' => [
        'factories' => [
            Controller\IndexController::class => Controller\Factory\IndexControllerFactory::class,
        ],
    ],
    'service_manager' => [
        'factories' => [
            ExampleManager::class => Service\Factory\ExampleManagerFactory::class,
        ],      
    ],
    'view_manager' => [
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => [
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ],
        'template_path_stack' => [
            __DIR__ . '/../view',
        ],
    ],
    'doctrine' => [
        'driver' => [
            __NAMESPACE__ . '_driver' => [
                'class' => AnnotationDriver::class,
                'cache' => 'array',
                'paths' => [__DIR__ . '/../src/Entity']
            ],
            'orm_default' => [
                'drivers' => [
                    __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
                ],
            ],
        ],
    ],
];

传递给 Application\Controller\IndexController::__construct() 的参数 1 必须是 Application\Service\ExampleManager 的实例,没有给出

它在本地工作似乎很奇怪,但不能从远程站点工作。是不是因为某种原因找不到我的服务路径?

【问题讨论】:

    标签: php zend-framework3


    【解决方案1】:

    已解决

    哇,好的,这是因为当我最初安装 zend-skeleton 时,我禁用了开发模式,但现有配置的缓存文件已在数据/缓存中创建。我在尝试思考本地安装和远程安装之间可能有什么不同时发现了这一点。解决方法是删除缓存文件。

    更好的解释和解决方案的功劳在这里: https://stackoverflow.com/a/45146213/5133172

    文件恢复为只读,开发模式被禁用,一切正常。 :)

    【讨论】:

    • 感谢所有帮助! :)
    猜你喜欢
    • 1970-01-01
    • 2017-04-06
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 2021-05-16
    • 2015-05-11
    • 2017-01-02
    相关资源
    最近更新 更多