Getting a pathname with .xml in it with YII? - yii

I have a controller called "SitemapController". This easily maps to this url:
/sitemap
However, I need that link to also work if you open
/sitemap.xml
How do I map it in YII so the system can use both?

you can achieve it like below by editing config file ,
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'sitemap/*'=>'sitemap/index',
'sitemap.xml/*'=>'sitemap/index',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
Now it will map routes to sitemap/someKey/someValue/..

Add this to your url routing 'sitemap' => array('site/sitemap', 'urlSuffix'=>'.xml'),

You can modify your url manager in config/main.php this way,
'site/sitemap.xml'=>'site/sitemap',
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'site/sitemap.xml'=>'site/sitemap',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),

In your .htaccess add this rule
RewriteEngine On
RewriteRule ^sitemap.xml.*$ http://example.com/sitemap/ [R=301,L]

Related

Creating own class for urlManager

These are my rules for urlManager. It works only for controller 'vijesti'. When I try to open any other page it gives me 404 error.
Should I write my own class for urls or what? Could someone explain me what to do?
'<controller:(vijesti)>/<action:(admin|create|update|delete|trazi)>' => 'vijesti/<action>',
'<kategorija:\w+>/<naslov:\w+>'=>'vijesti/view',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
Problem:
the rule '<kategorija:\w+>/<naslov:\w+>'=>'vijesti/view' covers '<controller:\w+>/<action:\w+>'=>'<controller>/<action>'
Try this:
'<controller:(vijesti)>/<action:(admin|create|update|delete|trazi)>' => 'vijesti/<action>',
'/<kategorija:\w+>/vw_<naslov:\w+>'=>'vijesti/view',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
try this:
'vijesti/<_a:(admin|create|update|delete|trazi|view)>' => 'vijesti/<_a>',
'<kategorija:\w+>/<naslov:\w+>'=>'vijesti/view',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

How to change the path of URL in yii

I have a question about yii URL.
For example, http://localhost/example/index.php?r=site/login, how could I change it to http://localhost/example/login?
I am able to change it to http://localhost/example/index.php/login by editing protected/config/main.php.
Below is my urlManager setting.
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false, //<-added as suggested by Martin
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'login' => 'site/login',
),
),
My question is how can I remove(hiding) the 'index.php' in http://localhost/example/index.php/login ?
SOLVED
I move .htaccess file from inside protected to root directory, where index.php is.
www
- protected
- index.php
- .htaccess
Can refer to http://www.yiiframework.com/wiki/214/url-hide-index-php/
Add “'showScriptName' => false,” to the rules array.

controller and module with same name

I have controller and module with same name: download
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'http://'.SITE_DOMAIN.'/<action:(download)>/<url:.*>'=>'<action>',
'http://<module:(download)>.'.SITE_DOMAIN.'/<code:\w{32}>'=>'<module>',
),
)
So I want to links like: http://domain.com/download/dir1/dir2/file1.zip
to be routed to: application/controllers/DownloadController
where $_GET['url']=='dir1/dir2/file1.zip'
And links like: http://download.site.com/some_code
to be routed to: application/modules/download/controllers/DefaultController.php
where $_GET['code']=='some_code'
But now both types of links are routed to: application/modules/download/controllers/DefaultController.php
I can't understand why
Try with this:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'http://'.SITE_DOMAIN.'/download/<url:.*>'=>'Download/index',
'http://download.'.SITE_DOMAIN.'/<code:\w{32}>'=>'Download/Default/index',
),
)
Note: both URLs will be routed to the index action of they own controller.

Yii urlManager parameter

I am trying to use the following url: (With the htaccess applied)
/user/{username}
to redirect to something like:
/user/view/{username}
I have no clue what the rule should be...
My current urlManager config is:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'caseSensitive'=>false,
'rules'=>array(
)
),
I'm also going to want some overrides such as (That go to the regular action)
/user/settings
/user/edit
etc... how would I add these?
Figured it out on my own!
'user/settings' => 'user/settings',
'user/<action>/*' => 'user/view/user/<action>',

Static Pages in Yii Problem

'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<view>' => array('site/page/view/'),
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
//'index' => array('site/index'),
),
),
I currently have that in my main.php file.
The problem I have is that when I view /index.php/index I have shown the index page in the pages folder but when I get to /index.php/about I still get the index.php file in the pages folder.
'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>',
//'index' => array('site/index'),
'<view>' => array('site/page/view/'),
),
),
It should have been like this:
Now the rule should be (at least in version 1.1.12)
'<view:\w+>' => 'site/page',
This code will provide $_GET['view'] to SiteController::actionPage, e.g. http://example.com/test will set $_GET['view'] = 'test'