Error creating Module zend framework

0

I created a new module in the zend, but it gives as 404 error.

modules.config.php

return [
    'Zend\Router',
    'Zend\Validator',
    'Application',
    'Album' <-- módulo que foi criado.
];

module.config.php

namespace Album;

use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;

return [
    'controllers' => [
        'factories' => [
            Controller\AlbumController::class => InvokableFactory::class,
        ],
    ],

    // The following section is new and should be added to your file:
    'router' => [
        'routes' => [
            'album' => [
                'type'    => Segment::class,
                'options' => [
                    'route' => '/album[/:action[/:id]]',
                    'constraints' => [
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ],
                    'defaults' => [
                        'controller' => Controller\AlbumController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
        ],
    ],

    'view_manager' => [
        'template_path_stack' => [
            'album' => __DIR__ . '/../view',
        ],
    ],
];

Module.php

namespace Album;

use Zend\ModuleManager\Feature\ConfigProviderInterface;

class Module implements ConfigProviderInterface
{
    const VERSION = '3.0.3-dev';

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

AlbumController

namespace Album\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class AlbumController extends AbstractActionController
{
    public function indexAction()
    {
        return ViewModel();
    }
}

Folder structure:

-Album
   -config
       -module.config.php
   -src
       -Controller
           -AlbumController.php
       -Module.php
   -view
       -album
           -album
               -index.phtml
       -error
       -layout
    
asked by anonymous 10.10.2017 / 15:27

1 answer

0

Good afternoon! What version of Zend Framework are you using? if it is above 3.0, you need, in addition to the steps you used, create the entry for your new module also in the composer.json file and run a dump-autoload.

This is due to the fact that Zend Framework 3 complies with PSR-4 , that is , the autoload of the classes is all dealt with by the composer.

Your Composer.json should look something like this:

{
...
"autoload": {
    "psr-4": {
        "Common\": "module/Common/src/",
        "Album\": "module/Album/src/"
    }
},
...
}

Remember to run the dump-autoload composer on the terminal after changing the file. Here has a good documentation on the ZF3 autoloader .

Edit:

Complementing the answer with the items discussed in the comments below: First in modules.config.php, it includes the complete namespace of the controller, thus:

<?php use Zend\Router\Http\Segment; use Zend\ServiceManager\Factory\InvokableFactory;

return [
    'controllers' => [
        'factories' => [
            \Album\Controller\AlbumController::class => InvokableFactory::class,
        ],
    ],

    // The following section is new and should be added to your file:
    'router' => [
        'routes' => [
            'album' => [
                'type'    => Segment::class,
                'options' => [
                    'route' => '/album[/:action[/:id]]',
                    'constraints' => [
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ],
                    'defaults' => [
                        'controller' => \Album\Controller\AlbumController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
        ],
    ],

    'view_manager' => [
        'template_path_stack' => [
            'album' => __DIR__ . '/../view',
        ],
    ], ];

Then in the controller, I correct the return of the indexAction method so that it returns an instance of the ViewModel

<?php
namespace Album\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class AlbumController extends AbstractActionController
{
    public function indexAction()
    {
        return new ViewModel();
    }
}

As I mentioned below, I tested on a new project here and everything worked correctly, following the steps outlined in the comments.

See if it solves that.

Edit 2:

Well, let's go to another edit, one thing I noticed in your git project is that you used the zend 2 directory structure, in zend 3 it changed a bit, so it is not finding the controller, reading this link , here is the structure that zend 3 is using.

In your question you pointed out this directory structure:

-Album
   -config
       -module.config.php
   -src
       -Controller
           -AlbumController.php
       -Module.php
   -view
       -album
           -album
               -index.phtml
       -error
       -layout

But your project was like this:

 -Album
       -config
           -module.config.php
       -src
           -Album
               -Controller
                   -AlbumController.php
       -view
           -album
               -album
                   -index.phtml
           -error
           -layout
       -Module.php

The differences above are, in ZF3 there is no longer the directory with the name of the module within SRC, so we directly put the directories Controller, Factory etc. within src. Another difference to note is the location of the Module.php file, which needs to be inside need to be inside the src directory in ZF3.

Another detail I noticed was also in your indexAction (), the return of an action in the zend should always (at least as far as I know ... rs) be an instance of Zend \ View \ Model \ ViewModel or Zend \ View \ Model \ JsonModel.

Finally, your Module.php also needed an adjustment in the getConfig method, to reflect the directory change.

Now it has to look like this:

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

And your module.config.php file will also generate an error in rendering the view because it has the wrong directory path:

Yours is like this:

'view_manager' => [
        'template_path_stack' => [
            'album' => __DIR__ . '/view',
        ],
    ], 

and it should look like this:

'view_manager' => [
        'template_path_stack' => [
            'album' => __DIR__ . '/../view',
        ],
    ], 

Because the view directory is a brother directory of the config, that is, it is in the same hierarchy, so the DIR constant will return the location of the config, you have to climb one level in the hierarchy to get access to the view directory.

With these last changes I mentioned above, your project worked here, see if you can run it around.

Hugs!

    
10.10.2017 / 17:55