ZF3 controller not found in module.php

981 Views Asked by At

I am very new in Zend Fw 3. I followed https://docs.zendframework.com/tutorials/ I added the controller config in module.php

public function getControllerConfig(){
    return[
        'factories' = >[
            Controller\DocumentController::class = > function($container) {
                return new Controller\DocumentController(
                    $container->get(Model\DocumentTable::class)
                );
            },
        ],
    ];
}

By running this, I faced this error:

ERROR

2

There are 2 best solutions below

0
On BEST ANSWER
<?php
     namespace Document;

     use Zend\Db\ResultSet\ResultSet;
     use Zend\Db\TableGateway\TableGateway;
     use Zend\ModuleManager\Feature\ConfigProviderInterface;

     class Module implements ConfigProviderInterface
     {


        public function getConfig()
        {
           return include __DIR__ . '/../config/module.config.php';
        }

        public function getServiceConfig()
        {
           return [
                'factories' => [
                        Model\DocumentTable::class => function($container) {
                            $tableGateway = $container->get(Model\DocumentTableGateway::class);
                            return new Model\DocumentTable($tableGateway);
                        },
                        Model\DocumentTableGateway::class => function ($container) {
                            $dbAdapter = $container->get(AdapterInterface::class);
                            $resultSetPrototype = new ResultSet();
                            $resultSetPrototype->setArrayObjectPrototype(new Model\Document());
                            return new TableGateway('document', $dbAdapter, null, $resultSetPrototype);
                        },
                ],
           ];
        }

        public function getControllerConfig()
        {
           return [
                'factories' => [
                        Controller\DocumentController::class => function($container) {
                            return new Controller\DocumentController(
                                    $container->get(Model\DocumentTable::class)
                            );
                        },
                ],
           ];
        }

}
0
On

Try implementing the AutoloaderProviderInterface as below:

<?php

namespace Document;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;

class Module implements ConfigProviderInterface, AutoloaderProviderInterface
{
    public function getConfig()
    {
       return include __DIR__ . '/../config/module.config.php';
    }

    public function getServiceConfig()
    {
       return [
            'factories' => [
                    Model\DocumentTable::class => function($container) {
                        $tableGateway = $container->get(Model\DocumentTableGateway::class);
                        return new Model\DocumentTable($tableGateway);
                    },
                    Model\DocumentTableGateway::class => function ($container) {
                        $dbAdapter = $container->get(AdapterInterface::class);
                        $resultSetPrototype = new ResultSet();
                        $resultSetPrototype->setArrayObjectPrototype(new Model\Document());
                        return new TableGateway('document', $dbAdapter, null, $resultSetPrototype);
                    },
            ],
       ];
    }

    public function getControllerConfig()
    {
       return [
            'factories' => [
                    Controller\DocumentController::class => function($container) {
                        return new Controller\DocumentController(
                                $container->get(Model\DocumentTable::class)
                        );
                    },
            ],
       ];
   }

   public function getAutoloaderConfig()
   {
       return [
           'Zend\Loader\StandardAutoloader' => [
               'namespaces' => [
                   __NAMESPACE__ => __DIR__,
               ],
           ],
       ];
   }
}

You might need to change the path in the line __NAMESPACE__ => __DIR__, depending on the directory structure you are using, and the location of the Module.php file.