简体   繁体   中英

how to do pretty url in zend framework?

i am wondering how can i do pretty urls with zend framework?

some background:

my url right now is: www.website.com/profile/43

the database structure is:

id   userid    name
1    43        John
..   ...       ...

and my router is:

$router->addRoute('getUserid',
            new Zend_Controller_Router_Route(
                    '/profile/:id',
                    array(
                            'module' => 'profile',
                            'controller' => 'social',
                            'action' => 'index'

                    )
            )
    );

obviously i would like the url to be: www.website.com/profile/John

from what research i mane it looks like i can do a lot of things with the router, add regexp, etc, but nothing on how to replace the id like in my case.

I need to do a query to find out the name and i was thinking to do it in the predispatch and use the result as a default variable, but i don't know the id since the $front->getRequest()->getParams() is not available :

$name = // do query to find out the name;

$router->addRoute('getUserid',
            new Zend_Controller_Router_Route(
                    '/profile/:id',
                    array(
                            'id' => $name,
                            'module' => 'profile',
                            'controller' => 'social',
                            'action' => 'index'

                    )
            )
    );

maybe im going at this from the wrong point. Any ideas?

thanks.

It's more general than ZF specific question.

What you can do here to make the URL nice AND save your URL with high rankings is implemented in WordPress and called slugs. Slug is just the post title (just spaces replaced by hyphen and etc). But obviously there may be two posts with the same title (and, automatically, slug). So if the slug for the new post already exists it is appended by number '1' at the end. If it also exists, it is appended by '2' and repeats till it gets something unique. Obviously, you need to add a column in the table for the slug.

So in your case the first user with name John has the url www.website.com/profile/John . The next user with the name John has the url www.website.com/profile/John1 and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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