pay attention to that ANYTHING_ELSE
So, I have my controllers and actions that I want to behave as normal in response to examples like this:
// for UserContoller with actionList and actionEdit
user/list
user/edit/25
But for everything that doesn’t fall under specific controllers and actions i want them to fall under one default controller and action like: BlogController and actionView. That is where ANYTHING_ELSE comes.
// ANYTHING_ELSE can be:
this-is-a-test-page
this/is/another/page/with/lots/of/slashes
this-has-extension.html
'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>',
'ANYTHING_ELSE' => 'blog/view',
),
),
I shall explain step by step how to get this working.
Step 1 – Create an Yii web app
Navigate to your Yii framework path in your console and create a new webapp. In my case I used this in my console:
where c:\zeus\yii-1.1.10.r3566\framework is my path to Yii php framework and c:\zeus\www\yiiblog is the path to my Yii webapp test folder
Stept 2 – fake my domain to dev.yiiblog.com
Go to C:\Windows\System32\drivers\etc and edit your hosts file by adding this line:
Step 3 – alter apache httpd.conf file
and restart apache service. I used in my windows console:
where my Apache 2 service is named “apache” not “apache2.2” like the default one.
Step 4 – create a database and configure a database connection into Yii
I’ve created a database yiitest and a user yiitest. Then I opened my Yii configuration file located ad /protected/config/main.php and edited the connection to MySQL:
Step 5 – download dburlmanager Yii extension
Go to Yii dburlmanager, download the Yii dburlmanager extension http://www.yiiframework.com/extension/dburlmanager/ and extract it to your /protected/extensions folder
Step 6 – Create MySQL database tables and add dummy data
Step 7 – Create your Yii custom Controllers
Create under /protected/controllers folder two php files named ArticleController.php and PageController.php:
ArticleController.php content:
PageController.php content:
Step 8 – create your custom Yii views
Create your view files corresponding to those controllers above with the path /protected/views/article/view.php and /protected/views/page/view.php:
Article view content:
Page view content:
Step 9 – add custom Yii url rules
Open again your main.php Yii config file and set your urlManager to something similar to:
Step 10 – create .htaccess file
Create a .htaccess file under your web app root and etid its content to:
Step 11 – test your SEO Friendly URLs
etc
Have fun creating awesome blogs or other web apps with complete url managing power.