0

我正在尝试使用 zend 框架 2 创建 restful api 模块。

我无法创建正确的 module.config.php

我的新模块的文件夹结构是:

[...]/module/Api
[...]/module/Api/config
[...]/module/Api/src/Api/Controller

我的控制器名为: ShortenerController.php 位于 [...]/module/Api/src/Api/Controller/ShortenerController.php

在其中我将命名空间设置为:

namespace Api\Controller;

在 [...]/module/Api/config 我有 module.config.php 文件,其中包含以下代码:

<?php
return array(
    'controllers' => array(
        'invokables' => array(
            'Api\Controller\Shortener' => 'Api\Controller\ShortenerController',
        ),
    ),

    // The following section is new and should be added to your file
    'router' => array(
        'routes' => array(
            'Api' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/api/s/[:url]',
                    'defaults' => array(
                        'controller' => 'API\Controller\Shortener',
                    ),
                ),
            ),
        ),
    ),


  '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',
//             'index/index'   => __DIR__ . '/../view/index/index.phtml',
//             'error/404'     => __DIR__ . '/../view/error/404.phtml',
//             'error/index'   => __DIR__ . '/../view/error/index.phtml',
//         ),
//         'template_path_stack' => array(
//             'application' => __DIR__ . '/../view',
//         ),
        'strategies' => array(
            'ViewJsonStrategy',
        ),
    ),
);

当我尝试调用 curl 并将数据发布到此链接时:

http://server_address/api/s/

我收到这样的错误:

PHP Fatal error:  Class 'Api\\Controller\\ShortenerController' not found in /home/ertai/zf/library/Zend/ServiceManager/AbstractPluginManager.php on line 170

我在这里做错了什么?我无法掌握我应该在 module.config.php 文件中写入什么以获得正确的路由。

4

1 回答 1

0

您似乎忘记application.config.php像这样声明您的模块:

return array(
    'modules' => array(
        'Application',
        'Api',
    ),

顺便说一句,请注意您的默认路由配置

                'defaults' => array(
                    'controller' => 'API\Controller\Shortener',
                ),

这是区分大小写的,所以它应该是:

                'defaults' => array(
                    'controller' => 'Api\Controller\Shortener',
                ),

我建议您也像这样设置默认操作:

                'defaults' => array(
                    'controller' => 'Api\Controller\Shortener',
                    'action' => 'index',
                ),
于 2013-06-06T15:11:05.147 回答