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

醉酒当歌 提交于 2019-12-07 03:09:42

问题


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

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"

edit

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

I am not looking for solution via .htaccess


回答1:


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')
    ));
}

from your loginAction() use:

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

Simple?



来源:https://stackoverflow.com/questions/12196330/how-to-add-variable-at-the-start-of-the-url-in-zend-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!