ZF2-我自己的应用中安装了ApiGility-路由不起作用 [英] ZF2 - ApiGility installed in my own app - route not working
问题描述
我正在尝试在我的应用中安装APIGILITY.我遵循了本教程:
https://apigility.org/documentation/recipes/apigility-in-existing-zf2-application
当我尝试访问apigility管理员:www.myapp.dev/apigility时,出现 请求的URL无法通过路由匹配" 错误./p>
我的配置如下:
'modules' => array(
'DoctrineModule',
'DoctrineORMModule',
'ZfcRbac', //Keep this at the top
'Application', //The applications main functions run from this module
//APIGILITY
'ZF\Apigility',
'ZF\Apigility\Provider',
'AssetManager',
'ZF\ApiProblem',
'ZF\MvcAuth',
'ZF\OAuth2',
'ZF\Hal',
'ZF\ContentNegotiation',
'ZF\ContentValidation',
'ZF\Rest',
'ZF\Rpc',
'ZF\Versioning',
'ZF\DevelopmentMode',
'ZF\Apigility\Admin',
'ZF\Configuration',
我已启用开发人员模式.
通常,如果存在路由并且ZfcRbac阻止了该路由,则会将我重定向.在这种情况下,当路由不可访问时,我会收到错误消息.
有一种简单的方法可以测试吗?
我通过以下操作解决了此问题:
本教程没有提及将ApiGility模板复制到您的应用程序.您需要这样做.我所做的就是将模板添加到我的application/config/module.config.php文件中.
return [
'view_manager' => [
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/exception',
'template_map' => [
'customer/layout' => __DIR__ . '/../view/layout/customer-layout.phtml',
'api/layout' => __DIR__ . '/../view/layout/api-layout.phtml',
'layout/layout' => __DIR__ . '/../view/layout/admin-layout.phtml',
在应用程序"模块中,我检查路由并相应地切换模板:
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$e->getApplication()->getEventManager()->attach(
MvcEvent::EVENT_ROUTE, function(MvcEvent $e) {
//Set the customer layout
$needle = $e->getRouteMatch()->getParam('controller');
$haystack = [
/* Customer template routes */
];
if (in_array( $needle , $haystack )) {
$e->getViewModel()->setTemplate('customer/layout');
}
//Apigility route
$haystack = [
'zf-apigility/ui'
];
if (in_array( $needle , $haystack )) {
$e->getViewModel()->setTemplate('api/layout');
}
}
);
}
要访问权限"页面,我现在通过以下方式访问: http://www.myapp.com/apigility/ui#/
希望这可以帮助某人...
I am attempting to install APIGILITY in my app. I have followed this tutorial:
https://apigility.org/documentation/recipes/apigility-in-an-existing-zf2-application
When I attempt to access the apigility admin: www.myapp.dev/apigility I get a "The requested URL could not be matched by routing" error.
My config is as follows:
'modules' => array(
'DoctrineModule',
'DoctrineORMModule',
'ZfcRbac', //Keep this at the top
'Application', //The applications main functions run from this module
//APIGILITY
'ZF\Apigility',
'ZF\Apigility\Provider',
'AssetManager',
'ZF\ApiProblem',
'ZF\MvcAuth',
'ZF\OAuth2',
'ZF\Hal',
'ZF\ContentNegotiation',
'ZF\ContentValidation',
'ZF\Rest',
'ZF\Rpc',
'ZF\Versioning',
'ZF\DevelopmentMode',
'ZF\Apigility\Admin',
'ZF\Configuration',
I have enabled developer mode.
Typically if a route exists and ZfcRbac is blocking the route, I am re-directed. In this case when the route is not accessible I get the error.
Is there a simple way to test this?
I solved this issue by doing the following:
The tutorial makes no mention of copying the ApiGility template to your app. You need to do this. What I did was to add the template to my application/config/module.config.php file.
return [
'view_manager' => [
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/exception',
'template_map' => [
'customer/layout' => __DIR__ . '/../view/layout/customer-layout.phtml',
'api/layout' => __DIR__ . '/../view/layout/api-layout.phtml',
'layout/layout' => __DIR__ . '/../view/layout/admin-layout.phtml',
In the Application module I check routing and switch the template accordingly:
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$e->getApplication()->getEventManager()->attach(
MvcEvent::EVENT_ROUTE, function(MvcEvent $e) {
//Set the customer layout
$needle = $e->getRouteMatch()->getParam('controller');
$haystack = [
/* Customer template routes */
];
if (in_array( $needle , $haystack )) {
$e->getViewModel()->setTemplate('customer/layout');
}
//Apigility route
$haystack = [
'zf-apigility/ui'
];
if (in_array( $needle , $haystack )) {
$e->getViewModel()->setTemplate('api/layout');
}
}
);
}
To access the apigility pages, I now access via: http://www.myapp.com/apigility/ui#/
Hope this helps someone...
这篇关于ZF2-我自己的应用中安装了ApiGility-路由不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!