
[英]zf2 how to use 3rd party module in my own specific module (based on route)?
[英]ZF2 - ApiGility installed in my own app - route not working
我正在尝试在我的应用中安装APIGILITY。 我遵循了本教程:
https://apigility.org/documentation/recipes/apigility-in-an-existing-zf2-application
当我尝试访问apigility管理员:www.myapp.dev/apigility时,出现“请求的URL无法通过路由匹配”错误。
我的配置如下:
'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阻止了该路由,则会将我重定向。 在这种情况下,当路由不可访问时,我会收到错误消息。
有没有简单的方法可以测试?
要跟踪HappyCoder自己的答案,您可以将zf-apigility模块中的所有路由与
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) {
// Route matched
$route_name = $e->getRouteMatch()->getMatchedRouteName();
// If apigility - set correct layout
if(preg_match('/^zf-apigility/', $route_name)) {
$e->getViewModel()->setTemplate('layout/api-layout');
}
}
);
}
这样做时-它将为所有功能视图设置适当的布局,包括/ apiligity(欢迎屏幕)
我通过以下操作解决了这个问题:
本教程没有提到将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');
}
}
);
}
要访问apigility页面,我现在通过以下网址访问: http ://www.myapp.com/apigility/ui#/
希望这可以帮助某人...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.