我正在尝试使用zend框架2创建restful api模块。
我无法创建适当的模块。config.php
新模块的文件夹结构是:
[...]/module/Api
[...]/module/Api/config
[...]/module/Api/src/Api/Controller
我的控制器名为:ShortenerController.php,位于[…]/模块/Api/src/Api/控制器/ShortenerController.php
在其中,我将命名空间设置为:
namespace Api'Controller;
[…我有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与post数据到这个链接:
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文件中写些什么来获得正确的路由
似乎您忘记在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',
),