开发者

Need to even further shorten easy urls using Zend Route

开发者 https://www.devze.com 2023-03-07 13:58 出处:网络
I might be asking a bit too much here but I would like to know how can I further shorten urls such that they look like there\'s not an inclusion of a controller.

I might be asking a bit too much here but I would like to know how can I further shorten urls such that they look like there's not an inclusion of a controller.

eg: I want to shorten this:

www.mysite.com/users/Bob-123

to

www.mysite.com/Bob-123

or something like www.mysite.com/bob-123-user

Any ideas here? - I'm using the 开发者_C百科Zend framework here


You can do with the help of Zend Router .

Here you need to fetch the usernames and add routes to it. This way it will recognise which controller and action it needs to fetch.

$router = $this->_front->getRouter();

$route = new Zend_Controller_Router_Route(
    ':username',
    array(
        'controller' => 'profile',
        'action'     => 'userinfo'
    )
);

$router->addRoute('user', $route);

An example taken from zend manual


In your application.ini you could do it using Zend_Controller_Router_Route_Regex:

resources.router.routes.user.route = "(\w+)-(\d+)"
resources.router.routes.user.type = "Zend_Controller_Router_Route_Regex" 
resources.router.routes.user.defaults.controller = users
resources.router.routes.user.defaults.action = index
resources.router.routes.user.map.1 = username
resources.router.routes.user.map.2 = id
resources.router.routes.user.reverse = "/%s-%d"

You would need to specify your own action and controller. Hope this helps.

0

精彩评论

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