Zend Framework 2 Album Tutorial 404 error occurred

2020-05-09 09:34发布

just posted this on IRC channel ZFTalk too,

Hope I can get some help on ZF2, ZF2 Album Tutorial, OSX using MAMP. Skeleton framework, homepage is working.

Issue : After completing section : 8.5 Listing albums, you fill up the module/Album/view/album/album/index.phtml with some code, then they ask you to preview the page on http://zf2-tutorial.localhost/album.

I get a 404, The requested URL could not be matched by routing.

I headed to Google for advice. Found a GIT repository with a 'fully working model' of the Tutorial, so i got this to compare my code with. If i set up this as another host I get the same 404 routing message.

After carefully studying the manual, it explicitly states in the start that you will not be able to view anything other than the start/home page if your httpd.conf / AllowOverride is not set to FileInfo.

Decided to scan the whole machine for files called httpd.conf, just for in case the path to the one I changed is not used by MAMP when powering up the server.

So found 3, changed all of them (Although 3 we're found, I believe the correct route is /private/etc) My problem still exists in the code i wrote from the tutorial, as well as the GIT code of the 'working model'.

Has anyone encountered issues with this? found this on stackoverflow Zend Framework 2 .htaccess mamp pro which has similarities to my problem but has not resolved it. Can anyone in here help me?

Other routes taken involve : Checking for spelling mistakes in the code, checking the application.config.php has a route set up. Please advise? :)

Module.php

<?php
namespace Album;

use Album\Model\Album;
use Album\Model\AlbumTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;

class Module
{
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

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

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'Album\Model\AlbumTable' =>  function($sm) {
                    $tableGateway = $sm->get('AlbumTableGateway');
                    $table = new AlbumTable($tableGateway);
                    return $table;
                },
                'AlbumTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Album());
                    return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                },
            ),
        );
    }
}

module.config.php

<?php
return array(
    'controllers' => array(
        'invokables' => array(
            'Album\Controller\Album' => 'Album\Controller\AlbumController',
        ),
    ),
    'router' => array(
        'routes' => array(
            'album' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/album[/][:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Album\Controller\Album',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),
    'view_manager' => array(
        'template_path_stack' => array(
            'album' => __DIR__ . '/../view',
        ),
    ),
);

3条回答
Bombasti
2楼-- · 2020-05-09 09:49

You can solve is by configuring the apache settings in (httpd.conf) change the "AllowOverride None" to "AllowOverride All". Such setting permit you to override the config value by .htaccess

查看更多
孤傲高冷的网名
3楼-- · 2020-05-09 10:00

Resolved this error with help from this thread.

I think, barring any other misconfiguration, the source of this error - for me at least - was where to register the Album module.

The SKELETON Application comes with an Application MODULE. Those are two different things and they have have their own config folders:

config                      // SKELETON Application config
module/Application/config   // Application MODULE config

The module needs to be registered in the Skeleton Application config file provided with the skeleton, namely config/application.config.php and NOT by creating an application.config.php file in the Application Module config, e.g. module/Application/config/application.config.php.

查看更多
Rolldiameter
4楼-- · 2020-05-09 10:01

Please try this

If you see a standard Apache 404 error, then you need to fix .htaccess usage before continuing. If you’re are using IIS with the URL Rewrite Module, import the following:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [NC,L]

You now have a working skeleton application and we can start adding the specifics for our application. Please let me know if it worked fine from you.

查看更多
登录 后发表回答