php - How to rewrite urls in zf2 based on database values -


i want url mydomain.com/user/id or mydomain.com/user?id=somevalue redirected mydomain.com/user/username . facebook redirect url accountid username. if use of url fetch correct user profile. id , username unique particular user , in database have saved username corresponding user id. route using

    'userprofile' => array(              'type'    => 'segment',              'options' => array(                  'route'    => '/user[/:id]',                  'defaults' => array(                      'controller' => 'userprofile\controller\userprofile',                      'action'     => 'index',                  ),              ), 

in controller makes api call using username or userid receives , return data view model. want after redirection profile visible user.is possible htaccess or can done using routing? example or lead appreciated...

you can redirect in controller after finding user given id:

return $this->redirect()->toroute('user',array ('username' => <username>)); 

you can check in index action if id id or username, and

  • if username, fetch user object , show profile,
  • if id, fetch user object , redirect above

but able create 2 routes constraints on id part

for username: [a-za-z][a-za-z0-9_-]* id: [0-9]+ 

Comments