我正在处理一个 Zend Framework 2 应用程序,我似乎无法在其中正确路由或不知道将其路由到何处。
我有一个主机名 => webapp.foo-bar.com。我们决定在末尾添加一个 Subhost =>/app/,这个应用程序的名称叫做 => app。我在页面上有一个链接,它的路径是 =>/graph/page-name。但是当我将鼠标悬停在看起来像这样的链接上时:
<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 部分中也有效。关于指向另一个 Controller 的链接未显示链接中的操作,我遇到了一个小问题。
<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的Controller中配置还是在this-url()中可以配置?
感谢您提供的所有帮助。真的很欣赏!
最佳答案
如果两个应用共享同一个域,则您的配置无效。我的配置文件版本如下。
附言我创建了单独的模块示例,因此它不会干扰应用程序配置。
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 路由无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44590628/