I'm trying to create restful api module with zend framework 2.
I'm unable to create proper module.config.php
My folder structure for new module is:
[...]/module/Api
[...]/module/Api/config
[...]/module/Api/src/Api/Controller
My controller is named: ShortenerController.php located in [...]/module/Api/src/Api/Controller/ShortenerController.php
In it I have namespace setted as:
namespace Api\Controller;
In [...]/module/Api/config I have module.config.php file which has this code:
<?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',
),
),
);
When I'm trying to invoke curl with post data to this link:
http://server_address/api/s/
I'm getting error such as this:
PHP Fatal error: Class 'Api\\Controller\\ShortenerController' not found in /home/ertai/zf/library/Zend/ServiceManager/AbstractPluginManager.php on line 170
What I'm doing here wrong? I can't get a grasp on what I should write into module.config.php file in order to have proper route.
It seems that you forgot to declare your module in application.config.php
like this :
return array(
'modules' => array(
'Application',
'Api',
),
And by the way, take care in your default route configuration
'defaults' => array(
'controller' => 'API\Controller\Shortener',
),
This is case-sensitive, so it should be :
'defaults' => array(
'controller' => 'Api\Controller\Shortener',
),
And I advise you to set a default action too like this :
'defaults' => array(
'controller' => 'Api\Controller\Shortener',
'action' => 'index',
),
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.