简体   繁体   English

如何在Zend Framework中的URL开头添加变量?

[英]How to add variable at the start of the URL in Zend Framework?

I am trying here to create url, like: 我在这里尝试创建网址,如:

/admin/login
/moderator/login

both the request would be served by same controller & action made for login, ie /account/login/<type> 请求将由同一个控制器和登录操作提供,即/account/login/<type>

Currently, all I am able to make the following URL: 目前,我可以制作以下网址:

/login/admin

/login/moderator

My current config file looks like the following: 我当前的配置文件如下所示:

resources.router.routes.login.route = "login/:type"
resources.router.routes.login.defaults.controller = "account"
resources.router.routes.login.defaults.action = "login"

I can't figure out, on how to put the type variable at the start of URL, I tried it but it gives server error. 我无法弄清楚,如何将type变量放在URL的开头,我尝试了但是它给出了服务器错误。 edit 编辑

I got this done, but can't understand why this didn't work earlier: 我完成了这个,但无法理解为什么这不能更早地工作:

resources.router.routes.login.route = ":type/login"
resources.router.routes.login.defaults.controller = "account"
resources.router.routes.login.defaults.action = "login"

edit 编辑

Is it possible to make this arrangement more flexible, so that: /login => /admin/login 是否可以使这种安排更加灵活,以便: /login => /admin/login

I am not looking for solution via .htaccess 我不是通过.htaccess寻找解决方案

Add the following to your Bootstrap.php 将以下内容添加到Bootstrap.php中

function _initRoutes() {
    $front_controller = Zend_Controller_Front::getInstance();
    $router = $front_controller->getRouter();

    $router->addRoute('login', new Zend_Controller_Router_Route(
        '/:type/login', array('controller' => 'account', 'action' => 'login')
    ));
}

from your loginAction() use: 从您的loginAction()使用:

$this->getRequest()->getParam('type');

Simple? 简单?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM