Url Re-Writing with YII - php

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'
),
),
);

Related

How to create pretty url in yii for module

I have searched a lot and tried a lot to do this , but no luck . I have referred this LINK. But it didn't solve my problem . I have a user module installed in yii (yii 2) . and i have user profile url like
http://192.168.1.31/Eb/user/profile?uguid=ac0c4558-77fc-4896-9b30-f77afe4d81cd
but i want this to like
http://192.168.1.31/Eb/user/profile/ac0c4558-77fc-4896-9b30-f77afe4d81cd
I mean without guid query string parameter .
I am trying in url manager like this
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:\w+>/<action:\w+>/<guid:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
How it is possible ??
In Yii UrlManager keys of rules parameter (attribute) must be regex pattern.
If I understand true your url have a structure CONTROLLER/ACTION (in my case Eb is subfolder), so your rules must be:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:\w+>/<action:\w+>/<uguid:\w+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
)
Be careful, that your uguid parameter not number so you must use w+ instead of d+
So you can create urls using createUrl:
Yii::app()->createUrl('user/profile', array('uguid' => 'USER_ID'))
Result must give which you want.
Your current url can also be accessed using
http://192.168.1.31/Eb/user/profile/uguid/ac0c4558-77fc-4896-9b30-f77afe4d81cd
You can remove your controller name by adding the following line in urlmanager rules:
'/Eb/user/profile/<id:([0-9a-zA-Z_\-]+)' => 'Eb/user/profile/uguid/<id>'
That should do the trick :)

Yii framework : controllerMap can 'rename' url, how about the view

My situation :
I have a these MVC file :
controllers\BookController.php
views\book\index.php
models\book.php
Then my client tell me they want to change the url
into "http://examplate.com/theBook/someAction"
instead of "http://examplate.com/Book/someAction"
A. controllerMap
I can change the it by using "controllerMap", but it say "cannot find the requested view" which I need to rename the view folder... which is dumd.
B. urlManager
When I try urlManager :
'theBook' => 'Book' //works
'theBook/<action:\w+>' => 'Book/<action>' //not working
Is there any way to change it all the MCV's routing url name, without rename my existing folder ?
In config -> main.php, change the following
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'caseSensitive'=>false,
'rules'=>array(
'/Book/<action>' => 'theBook/<action>/'
),
),

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/'
),
),

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

Yii Framework - URL Manager and Rules for routing

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

Categories