1
votes

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
ok what was happening when it wasn't working before you added my suggestions? i mean in what way was it not working? were you shown any errors?bool.dev
I wasnt working either. It works with usual syntax but throws error in url routing. I can easily open xyz.com/site/journey or xyz.com/site/invite but xyz.com/journey or xyz.com/journey/invite throws the error given above.Jashwant
My htaccess file has this code to hide index.php file. Options +FollowSymLinks IndexIgnore */* RewriteEngine on # if a directory or a file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # otherwise forward it to index.php RewriteRule . index.phpJashwant
your htaccess looks fine, i think it's a problem of open_basedir restriction, so take a look at that. an initial google search resulted in stackoverflow.com/questions/7328523/… . sorry i don't know much about that. after you have solved that problem, i think things will work fine.bool.dev
on some more google searching, i found more links to open_basedir restriction in yii, so you shouldn't have any problem in getting a solution.bool.dev

1 Answers

6
votes

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'.