Charanjeet Singh
Charanjeet Singh

Reputation: 45

create a multi level Yii route

I am trying to make a url with regex in YII1. I want to create the url for 'pagename/username'.

I tried like this :

'<pagename:([A-Za-z0-9-A-zÀ-ú]+)>/<i:w+>' => 'site/Profile',

but it is not working. Page is just reloading on itself

I want to show the new page when url become like 'pagename/username'.

Upvotes: 0

Views: 192

Answers (2)

Renziito
Renziito

Reputation: 78

I make it work this way

'public/<pagename:([A-Za-z0-9-A-zÀ-ú]+)>/<i>' => 'site/Profile'

see that i had to put a "public" at the start to make it works, and remove the +w of i , my "site/profile" just is a print_r of the $_REQUEST;

Array ( [pagename] => pagename [i] => username )

EDIT: The answer of @rob06 is so much better, just remember put it first of the array "rules"

'rules'     => array(
     '<pagename:[\w-]+>/<i:\w+>'              => 'site/profile',
     '<controller:\w+>/<id:\d+>'              => '<controller>/view',
     '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
     '<controller:\w+>/<action:\w+>'          => '<controller>/<action>',
            ),

Upvotes: 0

rob006
rob006

Reputation: 22174

It should be:

'<pagename:([A-Za-z0-9-A-zÀ-ú]+)>/<i:\w+>' => 'site/profile',

w+ will handle handle usernames containing only "w" letters, you need to use \w+ to handle any word.

Also [A-Za-z0-9-A-zÀ-ú]+ looks suspicious - you should avoid non-ASCII letters in URLs, so -A-zÀ-ú should be unnecessary. I would recommend to use this rule:

'<pagename:[\w-]+>/<i:\w+>' => 'site/profile',

Upvotes: 1

Related Questions