【问题标题】:Creating new Module in Zend Studio (Zend Framework 2)在 Zend Studio (Zend Framework 2) 中创建新模块
【发布时间】:2013-04-02 23:11:30
【问题描述】:

我一直在学习 Zend (http://www.youtube.com/watch?feature=player_embedded&v=EerB9bTvqrY) 的教程,但是当我在我的项目中添加一个新模块时,我无法导航到它,本教程中的说明是否不正确?

基本上,当我在 Zend Studio 中将一个新模块添加到我的 Zend Framework 项目时,我无法导航到它,我的新模块称为“交易”。我导航到 localhost/dealproject/deals 并收到错误 404。导航到 localhost/dealproject/ 时,它会正确加载 zend 框架应用程序页面。

感谢您的帮助。

module.config.php

<?php
return array(
'controllers' => array(
    'invokables' => array(
        'Deals\Controller\Deals' => 'Deals\Controller\DealsController',
    ),
),
'router' => array(
    'routes' => array(
        'deals' => array(
            'type'    => 'Literal',
            'options' => array(
                // Change this to something specific to your module
                'route'    => '/deals',
                'defaults' => array(
                    // Change this value to reflect the namespace in which
                    // the controllers for your module are found
                    '__NAMESPACE__' => 'Deals\Controller',
                    'controller'    => 'Deals',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                // This route is a sane default when developing a module;
                // as you solidify the routes for your module, however,
                // you may want to remove it and replace it with more
                // specific routes.
                '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(
                        ),
                    ),
                ),
            ),
        ),
    ),
),
'view_manager' => array(
    'template_path_stack' => array(
        'Deals' => __DIR__ . '/../view',
    ),
),

);

【问题讨论】:

  • 能否请您从导致问题的模块的 module.config.php 中向我们展示您的路由配置:)
  • 我现在已经在问题中发布了 module.config.php。感谢您的帮助。
  • 谢谢,我觉得不错:)。我在答案中发布了一些内容,希望它可以帮助您解决 404 :)

标签: php zend-framework2 zend-studio


【解决方案1】:

确保您已启用 ~/config/application.config.php 中的模块

'modules' => array(
    'Application',
    'Deals',
),

【讨论】:

  • 模块已启用,Zend Studio 在我创建模块时将其添加到 application.config.php 中。据我所知,一切都是正确的,我的 Zend Studio 安装有问题吗?
【解决方案2】:

对我来说,路由配置看起来是正确的,它应该会显示结果。我注意到您的 ZF2 应用程序的基本 URL (localhost/dealproject/) 位于域根目录的子目录中。您可能知道所有请求都映射到应用程序的 index.php (~/public/index.php)。这是通过您的.htaccess 文件(在同一目录中)中的一些配置来完成的。

在示例 URL www.example.com/blog 中,如果 blog 不作为 www.example.com 根目录下的文件夹存在,您将获得 404 Page not found。但是,如果没有找到目录,ZF2 的 .htaccess 会使 apache 调用域根目录中的 index.php(假设您使用 ZendSkeletonApplication)。

现在,由于您的 index.php 不在域根目录 (localhost/) 中,而是在 localhost/dealproject/ 中,并且您的 .htaccess (我假设)也在 localhost/dealproject/ 中,所以当您调用 localhost/dealproject/deals 时,apache 是寻找目录dealproject/deals,因为在localhost 中没有.htaccess 来准备必要的配置。

我建议您将dealproject 文件夹设为localhost 的根目录。这将使您能够像这样调用您的路由:localhost/deals 和 .htaccess 和 index.php 将被正确处理。

希望这会有所帮助:)

斯托扬

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多