zf2 专辑教程:不想在 uri 中键入索引

zf2 album tutorial : want to not have to type index in uri

我是 Zend Framework 2 的新手。这个问题与 Getting Started: A Skeleton Application

中的 "album" 教程有关

我用git安装,用composer安装依赖。

我复制了教程文件夹,设置数据库等

这一切似乎都有效,除了我必须输入完全限定的 url 才能进入索引页:

http://zf2-tutorial.localhost/album/index

...然后一切都按预期显示(我的相册,添加、编辑、删除表单有效)

我很确定只要输入

就可以正常工作

http://zf2-tutorial.localhost/album/

如果我遗漏了最后一段 index 我会得到这个错误:

A 404 error occurred Page not found.

The requested URL could not be matched by routing.

这是我的 module.config.php:

return array(
'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Album\Controller\Album',
                    'action'     => 'index',
                ),
            ),
        ),
        // The following is a route to simplify getting started creating
        // new controllers and actions without needing to create a new
        // module. Simply drop new controllers in, and you can access them
        // using the path /application/:controller/:action
        'application' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/application',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/[:controller[/:action]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                        ),
                    ),
                ),
            ),
        ),
    ),
),
'service_manager' => array(
    'factories' => array(
        'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
    ),
),
'translator' => array(
    'locale' => 'en_US',
    'translation_patterns' => array(
        array(
            'type'     => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo',
        ),
    ),
),
'controllers' => array(
    'invokables' => array(
        'Application\Controller\Index' => 'Application\Controller\IndexController'
    ),
),
'view_manager' => array(
    'display_not_found_reason' => true,
    'display_exceptions'       => true,
    'doctype'                  => 'HTML5',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index',
    'template_map' => array(
        'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
        'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
        'error/404'               => __DIR__ . '/../view/error/404.phtml',
        'error/index'             => __DIR__ . '/../view/error/index.phtml',
    ),
    'template_path_stack' => array(
        __DIR__ . '/../view',
    ),
),
)

如果我的虚拟主机很重要,这里是:

<VirtualHost 127.0.0.2:80>
 DocumentRoot "C:\xampp\htdocs\zend\public"

 RewriteEngine off

 ServerName zf2-tutorial.localhost
 ErrorLog "C:\xampp\htdocs\zend\error.log"
 SetEnv APPLICATION_ENV "ZF2_PATH"
 SetEnv ZF2_PATH
 <Directory "C:\xampp\htdocs\zend" >
    DirectoryIndex index.php
    AllowOverride All
    Order allow,deny
    Allow from all
 </Directory>
</VirtualHost>

您需要为操作设置 "default" 参数值。

        'child_routes' => array(
            'default' => array(
                'type'    => 'Segment',
                'options' => array(
                    'route'    => '/[:controller[/:action]]',
                    'constraints' => array(
                        'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                    'defaults' => array(
                        'action' => 'index'
                    ),
                ),
            ),
        ),