开发者

zend mod rewrite OR routing

开发者 https://www.devze.com 2023-01-23 16:53 出处:网络
I use Zend Framework in my web application. I have the next URL: domain/user/user/username. Where first user is a controller in the default开发者_高级运维 module. Second user is a GET variable. userna

I use Zend Framework in my web application. I have the next URL: domain/user/user/username. Where first user is a controller in the default开发者_高级运维 module. Second user is a GET variable. username is a value of this variable. All good. But for the good usability I want to have the next URL: domain/user/username. I know that by Zend GET rules it should be as my first variant. But maybe I can do this with mod rewriting or Zend_Route? Help me, please.

Thank you in advance.


Create /application/configs/routes.xml:

<?xml version="1.0" encoding="UTF-8"?>
<routes>
    <user>
      <route>user/:username/*</route>
      <defaults>
          <module>default</module>
          <controller>user</controller>
          <action>view</action>
          <username>0</username>
      </defaults>
    </user>
</routes>

Then in Bootstrap.php:

public function _initRouter()
{
    $front = Zend_Controller_Front::getInstance();
    $router = $front->getRouter();
    $config = new Zend_Config_Xml(dirname(__FILE__) . '/configs/routes.xml');
    $router->addConfig($config);

    return $router;
}

You may also do the same in a lot of other ways, e.g. in application.ini.


In your application.ini, add:

resources.router.routes.article.type = "Zend_Controller_Router_Route"
resources.router.routes.article.route = "user/:username/*"
resources.router.routes.article.defaults.module = "default"
resources.router.routes.article.defaults.controller = "user"
resources.router.routes.article.defaults.action = "view"
resources.router.routes.article.reqs.username = "guest"

and I suggest you to have a look at:

http://framework.zend.com/manual/en/zend.controller.router.html

0

精彩评论

暂无评论...
验证码 换一张
取 消