routesyii2url-patternyii-url-manager

Setup urlManager in Yi2 to work like twitter


Ho can I set up urlManager patterns in a way that it works like twitter or any other social networks? for example I want to have the following routes:

http://www.example.com/[user_id]
http://www.example.com/[user_id]/[mycontroller]/[myaction]

Solution

  • Add the following two URLs.

    The first rule will match any digit and execute the index action of the profile controller. The index action will receive the user ID, so the function will, most likely, look like public function actionIndex($user_id)

    '<user_id:\d+>' => 'profile/index',
    

    So /1 would execute actionIndex of the ProfileController and passing 1 as the $user_id.

    The second rule will match any digit followed by two words, separated by a slash of course.

    '<user_id:\d+>/<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    

    So /1/some/something would execute actionSomething in the SomeController. And the actionSomething would again receive the $user_id.