如何在 Zend Framework 中的 URL 开头添加变量? [英] How to add variable at the start of the URL in Zend Framework?

查看:26
本文介绍了如何在 Zend Framework 中的 URL 开头添加变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里尝试创建网址,例如:

I am trying here to create url, like:

/admin/login
/moderator/login

两个请求都将由同一个控制器提供服务为登录而进行的操作,即 /account/login/

both the request would be served by same controller & action made for login, i.e. /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"

我不知道如何将 type 变量放在 URL 的开头,我试过了,但它给出了服务器错误.编辑

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. 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"

编辑

是否可以使这种安排更加灵活,以便:/login => /admin/login

Is it possible to make this arrangement more flexible, so that: /login => /admin/login

我不是通过 .htaccess 寻找解决方案

I am not looking for solution via .htaccess

推荐答案

将以下内容添加到 Bootstrap.php

Add the following to your 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')
    ));
}

来自您的 loginAction()使用:

from your loginAction() use:

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

简单吗?

这篇关于如何在 Zend Framework 中的 URL 开头添加变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆