I'm getting a 404 error using Laminas Framework. Can anyone assist?

1.1k Views Asked by At

The error is:

A 404 error occurred Page not found.

The requested URL could not be matched by routing. No Exception available

I've registered my module and namespace with Composer and then ran Composer dump-autoload. My code is as follows:

module\Album\config\module.config.php

<?php

declare(strict_types=1);

namespace Album;

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

return [
    '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',
                    ],
                ],
            ],
        ],
    ],
    'controllers' => [
        'factories' => [
            Controller\AlbumController::class => InvokableFactory::class,
        ],
    ],
    'view_manager' => [
        'template_map' => [
            'album/index' => __DIR__ . '/../view/album/album/index.phtml',
        ],
        'template_path_stack' => [
            'album' => __DIR__ . '/../view',
        ],
    ],
];

module\Album\src\Module.php

<?php

declare(strict_types=1);

namespace Album;

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

module\Album\src\Controller\AlbumController.php

<?php

declare(strict_types=1);

namespace Album\Controller;

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

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

module\Album\view\album\album\index.phtml

Index page displays here...
1

There are 1 best solutions below

0
On

As you specified the root as

'route' => '/album[/:action[/:id]]',

The url should look like http://laminas.com/album which will route to the indexAction as you have defined that action as the default, but http://laminas.com/album/index would have the same effect.