Zend 3 Argument 1 passed to Application\Controller\IndexController::__construct() must be an instance of , none given,

隐身守侯 提交于 2019-12-10 20:12:28

问题


This configuration works on my local install, but not on remote site.

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'
                ],
            ],
        ],
    ],
];

Argument 1 passed to Application\Controller\IndexController::__construct() must be an instance of Application\Service\ExampleManager, none given

Seems strange for it to work locally, but not from a remote site. Is it not finding the path to my service for some reason?


回答1:


SOLVED

Wow, ok so this happened because when I had initially installed zend-skeleton awhile back I had disabled development mode, but cache files of the existing config had been created in data/cache. I discovered this while trying to think what could be different between my local and remote install. The fix is to remove the cache files.

A better explanation and credit for the solution is here: https://stackoverflow.com/a/45146213/5133172

Files are back to read only, dev mode is disabled and all works perfectly. :)



来源:https://stackoverflow.com/questions/49129078/zend-3-argument-1-passed-to-application-controller-indexcontroller-construct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!