Yii Framework - URL Manager and Rules for routing - php

I have these default route rules in my urlManager in Yii framework:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
I have a SiteController.php for all the /site/ requests. What I need now
is to redirect /home request to /site/index.
I've tried adding this array item:
'home' => '/site/index',
'/home' => 'site/index',
And none of them worked.
Can anyone solve this issue?

Just remove the extra '/' slashes. This worked for me when I tested it just now:
'home' => 'site/index',
I hope that helps.

'urlManager'=>array(
'urlFormat'=>'path',
'useStrictParsing'=>true,
'rules'=>array(
'' => 'site/index',
'Home'=>'site/index',
first one will show empty instead of home.
second one will show Home instead of site/index

Related

Yii 1.1 CLinkPager generated uncomplete url

I'm new in Yii 1.1. and i have a problem in generating url using CLinkPager.
The right url is http://example.com/read/group/car?page=2 [baseurl/controller/action/id?page_number] but yii 1.1 CLinkPager generated wrong url (http://example.com/read/group?page=2) and missed */car.
my config/main.php is like
...
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/*<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
...
How configure yii to get the right url?
Thanks in advance.
You're having a problem with your rules, replace them with:
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\w+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
You tried to use <id:\d+> but \d stands for digit. so you need to replace it with \w (word character)

Url Re-Writing with YII

I am trying to use the YII for URL Re-Writing.
Is there any way to show the page extensions like .html or .php?
Please help I am new in YII.
Use urlSuffix parameter
array(
......
'components'=>array(
......
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'pattern1'=>'route1',
'pattern2'=>'route2',
'pattern3'=>'route3',
),
'urlSuffix' => '.html'
),
),
);

URL rewriting with Yii framework

I have a url like
www.example.com/food/xyz
for which I have written a rule like this in the main.php (config folder)
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'caseSensitive'=>false,
'rules'=>array(
'/food/<name:\w+>/' => 'food/index/',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
This means that internally "xyz" will be passed as a parameter to the actionIndex of my FoodController according to the rule
class FoodController extends Controller
{
public function actionIndex($name){
//some code here
}
public function actionItems(){
//some code here
}
}
The problem i'm facing is that I have another method in the same class called actionItems and when I use the URL
www.example.com/food/items
it calls the actionIndex and passes "items" as a parameter
Any idea on how to solve this?
Thanks in advance.
Your first rule should be for the items
'/food/items'=>'food/items',
'/food/<name:\w+>/' => 'food/index/',
Alternatively, I would rather use an alias to split the use of the controller such that all 'food'/<:name>' urls are differentiated from '/food/action' urls for example:
'/food/<name:\w+>/' => 'food/index/',
'/foods/<action:\w+>' => 'food/<action>'
Try this, just change order:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'caseSensitive'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'/food/<name:\w+>/' => 'food/index/'
),
),

MongoDB Yii and the URL Manager

I'm having a bit of trouble with MongoID in and the url manager in Yii.
If I have a url like:
http://www.example.com/article/1
or
http://www.example.com/article/view/1
Everything works. Now for the problem. I am using MongoDB so my urls look like this:
http://www.example.com/article/50e18c655b0c4dce75000033
or
http://www.example.com/article/view/50e18c655b0c4dce75000033
And each scenario throws a 404 not found error, like it cannot go the actionView. My url manager in the config looks like this:
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>/<id:\d+>/<cid:\d+>'=>'<controller>/<action>',
),
),
What am I doing wrong with this and how do I get the MongoID to register as ids?
Had the same issue. Im my case it was invite system and it was very important to rule exactly hex, bcause I have InviteController as well.
'invite/<invite_key:[a-fA-F0-9-]+>'=>'site/invite',
in your case i suppose it should be something like this
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>/<id:[a-fA-F0-9-]+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>/<id:\d+>/<cid:\d+>'=>'<controller>/<action>',
),
),
Maybe you should change <id:[a-fA-F0-9-]+> to <cid:[a-fA-F0-9-]+>, I dont really know how your controller works.
I hope you found solution a bit earlier. But Google points here for the several request.

Hide all controllers from url

I've used Yii's urlManager to rewrite my url from mypage.com/controller/view to mypage.com/view. But I have more than one Controller and would like to apply this on all of them. Can this be done?
My current urlManager:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<action:\w+>' => 'site/<action>',
),
),
First way:
'<action:(login|logout|about)>' => 'site/<action>',
'<action:(view|edit)>' => 'product/<action>',
Second way to implement this feature is to use Using Custom URL Rule Classes

Categories