Student
Student

Reputation: 1863

Send params in URL in different way?

I don't know how to write title for this question. Anyway this is the scenario ..

I have a table like this

Person

id | username | firstname | lastname
 1 |    ab    |     Aaaa  |    Bbbb
 2 |    yz    |     Yyyy  |    Zzzz

I use following URL to show person profile:

example.com/person/profile/index?id=1   // Show person 1 profile
example.com/person/profile/index?id=1   // Show person 2 profile

Here: person is module, profile is controller and index is action used to get and show user profile using param id

Above scenario is working perfectly.

Question:

Now I want to use following URLs to get user profile using usernames

example.com/person/profile/ab   // Show person 1 profile
example.com/person/profile/yz   // Show person 2 profile

Any idea?

Upvotes: 0

Views: 108

Answers (2)

Starx
Starx

Reputation: 78971

By default you don't need to do anything. Just add /name/THENAME to the last of the url.

Then,

  1. Get the value of the name

    $name = $this -> _request -> getParam('name');
    
  2. Use that values to extract the id from the database

  3. And use it as you want

Upvotes: 1

Electronick
Electronick

Reputation: 1122

If you want stay using default ZF Route, just change link to example.com/person/profile/name/ab and get access to name from your controller with $this->getRequest()->getParam('name')

Also you can make specific Route, you can find documentation here: http://framework.zend.com/manual/en/zend.controller.router.html

Also you can configure custom route thought you config file using resources:

resources.router.routes.profile.route = "/person/profile/:name"
resources.router.routes.profile.defaults.controller = "person"
resources.router.routes.profile.defaults.action = "profile"
resources.router.routes.profile.reqs[] = "name"

Upvotes: 4

Related Questions