Yii basic url rewrite

2019-01-29 04:43发布

问题:

I am new to php, new to mvc, new to yii, and new to url-rewriting. So, I am sorry, if I am asking something very basic.

I have hide the index.php (from the htaccess method discussed in yii forums)

In my urlmanager, I have this,

'urlFormat'=>'path',
             'rules'=>array( 
             '<controller:\w+>/<id:\d+>'=>'view',
             '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
             '<controller:\w+>/<action:\w+>'=>'<controller>/<action>' 
              ),
             'showScriptName'=>false,

I have 3 files in view/site folder.

  1. 'journey',
  2. 'invite',
  3. 'linkedin'

Now, my home page should redirect to 'journey' action (i.e. should open the 'site/journey.php')

So, I guess, this would be

'/' => 'site/journey'

It works too.

Now, I want 'journey/invite' should invoke the 'invite' action i.e. should open 'site/invite.php'

And, 'journey/linkedin' should invoke the 'linkedin' action i.e. 'site/linkedin.php'.

but,

'journey/invite' => 'site/invite',
'journey/linkedin' => 'site/linkedin'

is not working.

Also, can someone help me understand this,

<controller:\w+>/<id:\d+>

i.e. what is controller in url and what does 'w+' mean ?

A reference to guide will help too.

Edited after bool.dev's suggestion:

Changed the code , as you said (I tried that earlier too, removing all default rules). Now my url manager is like,

    '/' => 'site/journey',
    'journey/invite' => 'site/invite',
    'journey/linkedin' => 'site/linkedin',
    '<controller:\w+>/<id:\d+>'=>'view',
    '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
    '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', 

But it throws an error

"Warning: require_once(): open_basedir restriction in effect. File(/var/xyz.com/../yii/framework/yii.php) is not within the allowed path(s): (/usr/share/php:/usr/share/pear:/usr/share/php/libzend-framework-php:/var/*/tmp:/var/xyz.com) in /var/xyz.com/journey.php on line 12 Warning: require_once(/var/xyz.com/../yii/framework/yii.php): failed to open stream: Operation not permitted in /var/xyz.com/journey.php on line 12 Fatal error: require_once(): Failed opening required '/var/xyz.com/../yii/framework/yii.php' (include_path='.:/usr/share/php:/usr/share/php/libzend-framework-php') in /var/xyz.com/journey.php on line 12'

when I do xyz.com/journey/invite or even xyz.com/journey

Edit:

It was a permission issue, @bool.dev's suggestion to put specific rules on top worked :)

回答1:

These two:

'journey/invite' => 'site/invite',
'journey/linkedin' => 'site/linkedin'

are not working because the url is being matched by a previous rule:

'<controller:\w+>/<action:\w+>'=>'<controller>/<action>'

To prevent that from happening just make sure that rule is mentioned last, before any specific(i.e not matched by regular expressions) rewrites, so your rules can look somewhat like this :

'journey/invite' => 'site/invite',
'journey/linkedin' => 'site/linkedin',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>'

This : \w+ means match any string with one or more occurrences of any “word” character (a-z 0-9 _) and \d+ means match any string with one or more occurrences of any digits (0-9) . Check out php regex.

Edit

Hadn't read your question thoroughly before, the controller in the rule is simply a name for the matched expression, so you could have had '<contr:\w+>/<act:\w+>'=>'<contr>/<act>'.

Edit2

After reading your edited question with the rules array, afaik you could use ''=>'site/journey' instead of '/'=>'site/journey'.