Error creating zend framework module

Asked

Viewed 341 times

0

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

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

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, to create the entry for your new module also in the file Composer.json and run a dump-autoload.

This is due to the fact that Zend Framework 3 obeys PSR-4, that is, the autoload of the classes is all handled by.

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 good documentation on the ZF3 autoloader.

Edit:

Complementing the response with the items discussed in the comments below: First in modules.config.php, it includes the full controller namespace, 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, fix the return of the indexAction method so that it returns an instance of the Viewmodel object

<?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 commented below, I tested on a new project here and everything worked correctly, following the steps described in the comments.

See if that’ll do it for you.

Edit 2:

Well, let’s go to another Edit, one thing I noticed about your project in git is that you used the directory structure of zend 2, in zend 3 it changed a little bit, so it’s not finding the controller, I recommend reading this one 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 with this:

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

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

Another detail I noticed was in its indexAction(), the return of an action in 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 he needs to be like that:

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 is with the wrong directory path:

Yours is like this:

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

and would need to be like this:

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

Because the view directory is a sister directory of config, that is, it is in the same hierarchy, thus being the constant DIR return the config location, you have to climb a level in the hierarchy to be able to access the view directory.

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

Hugs! Projeto Funcionando corretamente

  • I already did! But it’s not working anyway.

  • Did you run the dump-autoload Composer on the terminal as well? By the way, you need to run it inside the project folder, where is Composer.json Another detail, is your cache enabled? If yes, try clearing the data/cache directory

  • Rodei, appeared "Generating autoload files". I also tried to clear the cache. It doesn’t work yet.

  • Okay, I’ll try to simulate here in my environment... I’m copying all the classes you posted to find out

  • Well... come on, here in my simulations it worked, two notes here, first in the modules.config.php file, include before the controllers this: Album, ie full namespace ( Album Controller Albumcontroller::class), second remark, in your controller you are returning Viewmodelcomo if it was a function, and zend requires this return to be an instance of the Viewmodel class, ie missing the new reserved word before, would look like this: Return new Viewmodel(); - I will complement my answer with these items ;-)

  • Dude, file continues with the problem. I deleted the module and redo it all again, and it still didn’t work. What else could it be. It gives as if it were a route error, by not finding the page.

  • Is there a way to get this project into git or gist for us to see? Why here I copied exactly the same folder structure as you, saved with the same names, copied your source code... rs There must be something in the middle of the way cluttering the middle of the field

  • https://github.com/jonathandetoni/teste

  • Well... I edited the answer with some more notes I made in your source... rs

  • I went up with the tests I was trying, however, I made the corrections and committed again, and it still wasn’t. I will give up this framework and migrate to Windows

  • This is the error it gives me: The requested URL could not be Matched by routing.

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.