我想在我的ZF2应用程序中添加一条新路径(链接),如下所示:
mysite.com/somename/?invitecode=12345
请注意, / somename / 不应该是控制器,而只是链接中用于跟踪目的的名称。我想我可以通过添加一个新的控制器来做到这一点,但由于这个名称将是动态的,我不能使用控制器。我在module.config.php文件中找到了这个:
'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',
),
),
我尝试在此处添加一个路径,仅用于测试目的:
'application/index/indextest' => __DIR__ . '/../view/application/index/index.phtml',
我尝试过访问这样的网址:
mysite.com/index/indextest
但我可以访问此链接的唯一方法是在控制器中添加一个动作,如下所示:
public function indextestAction()
{
return $this->redirect()->toUrl("/");
}
请注意:
mysite.com/THISNAMEHERE/?invitecode=12345
请注意 THISNAMEHERE 是动态的,并根据我的vhost配置文件中的内容而有所不同。
编辑:
伙计我到目前为止已完成以下操作,我添加了一个名为“InviteController”的新控制器,它执行以下检查:
public function indexAction()
{
if(!empty(htmlspecialchars($_GET["inviter"])))
{
return $this->redirect()->toUrl("/user/emailsignup");
}
}
我已将控制器添加到invokables列表中,如下所示:
'Application\Controller\Invite' => 'Application\Controller\InviteController',
在我的module.config.php文件中:
'invite' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/invite/',
'defaults' => array(
'controller' => 'Application\Controller\Invite',
'action' => 'index',
),
),
),
所以现在当我尝试访问URL时,它就像是:
mysite.com/**invite**/?inviter=12345
然而,这仍然不是我想要的......我需要这个粗体部分(INVITE)是动态的。基本上,如果我从不同的vhost访问应用程序,它将是这样的:
mysite.com/vhost1name/?inviter=1234
我还是喜欢在该控制器中调用InviteController和Index动作。
编辑#2:终于解决了!感谢@Wilt的链接和解释! :)
万一有人想知道,这就是解决方案:
'application' => 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(
'__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(
'controller' => 'Application\Controller\Invite',
'action' => 'index',
),
),
),
),
答案 0 :(得分:1)
您的ZF2应用程序的路由在路由器配置中配置。 ZF2模块的路由器配置通常存储在模块module.config.php
文件夹内的config
文件中:
/module/MyModuleName/config/module.config.php
路由器配置如下所示:
// This is your router config
'router' => array(
'routes' => array(
// config goes here
),
),
您可以在ZF2文档章节 Routing and controllers
中阅读有关路由的更多信息您在问题中提到的是view_manager
配置而不是router
配置。
如果您不熟悉路由和路由器配置等基本概念,我强烈建议您在使用自己的应用程序之前按照教程了解这些基础知识。 ZF2 tutorial skeleton application (also known as the album application)是一个很好的起点。按照本教程中的步骤将帮助您实现您想要的目标。