php – 链接中的ZF2路由无法正常工作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 链接中的ZF2路由无法正常工作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4634字,纯文字阅读大概需要7分钟。
内容图文
![php – 链接中的ZF2路由无法正常工作](/upload/InfoBanner/zyjiaocheng/789/b6604f993c6849349e0ccb8748e489bc.jpg)
我正在研究Zend Framework 2应用程序的工作,我似乎无法正确路由或知道在哪里路由它.
我有一个Hostname => webapp.foo-bar.com.我们决定添加一个Subhost => / app /到最后,这个应用程序的名称叫做>应用程序.我在页面上有一个链接,它的路线会说=> /图/页名.但当我将鼠标悬停在看起来像这样的链接上时:
<a href="/graph/page-name">FooBar</a>
我得到webapp.foo-bar.com/graph/page-name而不是webapp.foo-bar.com/app/graph/page-name.
我的应用程序配置是:
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'hostname',
'options' => array(
'route' => 'webapp.foo-bar.com/app',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
),
// The following is a route to simplify getting started creating
// new controllers and actions without needing to create a new
// module. Simply drop new controllers in, and you can access them
// using the path /application/:controller/:action
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/application',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
'aliases' => array(
'translator' => 'MvcTranslator',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController'
),
),
'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',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
// Placeholder for console routes
'console' => array(
'router' => array(
'routes' => array(
),
),
),
);
我看到有一个选项,如:
<a href="<?=$this->url('application/default', array('controller' => 'graph', 'action' => 'page-name')?>">FooBar</a>
这需要在每个链接上完成,还是我们能够在配置级别完成?
谢谢!
更新:
我已经设法让客户端使用this-> url()方法,事情看起来很好.即使在jQuery部分,链接也在起作用.我有一个关于链接到另一个控制器没有显示链接中的操作的小问题.
<a href="<?php echo $this->url('foo-bar', array('action' => 'bar-foo'))?>?year=2015"</a>
带回webapp.foo-bar.com/app/foo-bar?year=2015.我希望它返回webapp.foo-bar.com/app/foo-bar/bar-foo?year=2015.这是foo-bar控制器中的配置还是可以在this-url()中完成?
万分感谢所有帮助.真的很适合它!
解决方法:
如果两个应用共享同一个域,则您的配置无效.我的配置文件版本如下.
附:我创建了单独的模块Sample,因此它不会干扰Application配置.
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use \Sample\Controller\IndexController;
use Zend\ServiceManager\Factory\InvokableFactory;
return [
'router' => [
'routes' => [
'v2' => [
'type' => Segment::class,
'options' => [
'route' => '/v2/app',
'defaults' => [
'controller' => IndexController::class,
'action' => 'index',
],
],
'child_routes' => [
'index' => [
'type' => Literal::class,
'options' => [
'route' => '/',
'defaults' => [
'controller' => IndexController::class,
'action' => 'index',
],
],
],
'application' => [
'type' => Segment::class,
'options' => [
'route' => '[/:action]',
'defaults' => [
'controller' => IndexController::class,
'action' => 'index',
],
],
]
],
],
],
],
'controllers' => [
'factories' => [
IndexController::class => InvokableFactory::class,
],
],
'view_manager' => [
'template_path_stack' => [
__DIR__ . '/../view',
],
],
];
这是您创建该应用程序的链接的方法:
<?php echo $this->url("v2/application", ['action' => 'page-name']) ?>
内容总结
以上是互联网集市为您收集整理的php – 链接中的ZF2路由无法正常工作全部内容,希望文章能够帮你解决php – 链接中的ZF2路由无法正常工作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。