how to rewrite url in Yii? - php

I have an action named contact in the site controller, so when I open it in the browser, the url displayed as: mysite.com/index.php/site/contact, I want to change it to be just the view name of the action, so it should be mysite.com/contact.php, how to do that ?

To remove the index.php from the url you'll need to add 'showScriptName' => false into your components urlManager array in /protected/config/main.php file:
// application components
'components'=>array(
...
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
...
),
...
),
...
),
That would make the route for your contant page mysite.com/site/contact. In order to remove the /site you'd need to edit the rules array, something like this may work;
// application components
'components'=>array(
...
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
...
'<action:\w+>.php'=>'site/<action>',
...
),
...
),
...
),
This would mean the url mysite.com/contact.php will render the site/contact route.
Edit
In answer to your question 'no i want it to render mysite.com/one, where one is a string id':
You've got to be careful with such short url rules, as broad rules like that will be picked up by other paths and reroute the user when you don't want them to be redirected. However, to redirect mysite/one to mysite/mycontroller/myaction?id=one you could use '<id:\w+>'=>'mycontroller/myaction'
But, as I said, be careful, this will reroute everything with a one word path. for example take this scenario:
You have a controller named user
You want to navigate to the index action of the user controller (route user/index)
You navigate to mysite.com/user (which would normally render mysite.com/user/index)
This would now instead redirect the user to mysite/mycontroller/myaction?id=user

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

Yii app()->createUrl for view action

I use generated actions by Gii in a module said News. I have normal view action that works with an id parameter (such as example.com/news/view/id/1).
When I use this line of code:
Yii::app()->createUrl("news/view",array("id"=>$data->primaryKey))
It generates example.com/news/1 (if $data->primaryKey is 1). It is not correct.
When I use this line of code:
Yii::app()->createUrl("news/view/id/",array("id"=>$data->primaryKey))
It generates example.com/news/id/id/1 (if $data->primaryKey is 1).
I am so confused! in first situation, this function doesn't generate id as a parameter name, and in second situation, it does! but after manually added id.
What shoud I do to make correct url format with this function?
Edit: news is a module. I changed the line of code as:
Yii::app()->createUrl("news/default/view/id/",array("id"=>$data->primaryKey))
It generates example.com/news/default/view/id/1 that is correct, but I don't want that default!
In config file you have 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+>'=>'<controller>/<action>',
),
),
this create how to look the URL.
You not need to write the id parameter when you create URL because is default. Look on the urlmanager rules:
Yii::app()->createUrl("news/view/",array("id"=>$data->primaryKey)) => example.com/news/id/1
On module defaut:
Yii::app()->createUrl('/news/default/view', array('id' => $data->primaryKey))
You need to create the urlmanager rule... how you want to look at your URL. More details here.
Use
'rules'=>array(
'news/<controller:\w+>/<id:\d+>'=>'news/<controller>/view',
'news/<controller:\w+>/<action:\w+>/<id:\d+>'=>'news/<controller>/<action>',
'news/<controller:\w+>/<action:\w+>'=>'news/<controller>/<action>',
),
if you have a module names news.
You can try to replace news with regex but you will face problems with urls matched by several regexes if your regex is too broad. Use something like <module:news|accounting|people> in rules array key and <module> in rules array value.
If you need more sophisticated url management, or if you cannot solve your task with regexes, you can always extend CUrlManager.
trying to check it on this directory: protected/config/main.php sir.
urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controll<>er>/<action>',
),
),
Please notice is that routed well? :)

How to rename Yii's controllers using urlManager Component

I need to access my controllers with another name. For example, my controller name is *form_number_98*, but I need to access it by calling it through URL some thing like */form_98*.
All above mentioned will be done by the following line:
'form_<id:\d+>'=>'form_number_<id>',
But what if I need to access to it's view or another actions?
For example access to view with id 1
http://example.com/form_98/view/1
Which means:
http://example.com/form_number_98/view/id/1
Your answers would be greatly appreciated.
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'caseSensitive'=>false,
'rules'=>array(
'/form_number_98/<action:\w+>/<id:\d+>' => 'form_98/<action>/'
),
),
You can achive it by using url manager. Imagine you have a controller called "SampleController" with an action called "actionSampless", then you can have something like this:
'urlManager'=>array(
'urlFormat'=>'path','showScriptName'=>false, 'caseSensitive'=>false,
'rules'=>array(
'test/test/*'=>'Sample/Sampless',
//'pattern1'=>'route1', (it goes like this)
),
),
for more info click here. read User-friendly URLs topic..
Hope it may help you.

How can I redirect to another entry script?

I have two entry scripts on my application, as specified here. How can I set the route to redirect /example.com/admin to example.com/backend.php
Thanks.
Take a look at the documentation about URL management:
http://www.yiiframework.com/doc/guide/1.1/en/topics.url
Without knowing the details of your setup, in your protected/config/main.php (if main.php is you application configuration file), you would need something along this:
// note: this is extracted from a project which uses 'friedly urls',
// depending on your setup, YMMV
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName' => false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'/admin'=>'/backend', // <-- define your custom routes/redirects here
),
),

Categories