· 6 years ago · Mar 07, 2019, 10:44 AM
1// for UserContoller with actionList and actionEdit
2user/list
3user/edit/25
4
5// ANYTHING_ELSE can be:
6this-is-a-test-page
7this/is/another/page/with/lots/of/slashes
8this-has-extension.html
9
10
11'urlManager' => array(
12 'urlFormat' => 'path',
13 'showScriptName' => false,
14 'rules' => array(
15 '<controller:w+>/<id:d+>' => '<controller>/view',
16 '<controller:w+>/<action:w+>/<id:d+>' => '<controller>/<action>',
17 '<controller:w+>/<action:w+>' => '<controller>/<action>',
18 'ANYTHING_ELSE' => 'blog/view',
19 ),
20),
21
22cd c:zeusyii-1.1.10.r3566framework
23yiic webapp c:zeuswwwyiiblog
24
25127.0.0.1 dev.yiiblog.com
26
27<VirtualHost *:80>
28 DocumentRoot "c:/zeus/www/yiiblog"
29 ServerName dev.yiiblog.com
30 ErrorLog "logs/dev.yiiblog.com-error.log"
31 CustomLog "logs/dev.yiiblog.com-access.log" common
32</VirtualHost>
33
34net stop apache
35net start apache
36
37'db'=>array(
38 'connectionString' => 'mysql:host=localhost;dbname=yiitest',
39 'emulatePrepare' => true,
40 'username' => 'yiitest',
41 'password' => 'password',
42 'charset' => 'utf8',
43),
44
45CREATE TABLE IF NOT EXISTS `articles` (
46 `seoURL` varchar(100) NOT NULL
47) ENGINE=InnoDB;
48
49INSERT INTO `articles` (`seoURL`) VALUES
50('first-post'),
51('another-post'),
52('post/value'),
53('website/page1');
54
55CREATE TABLE IF NOT EXISTS `pages` (
56 `seoURL` varchar(100) NOT NULL
57) ENGINE=InnoDB;
58
59INSERT INTO `pages` (`seoURL`) VALUES
60('page-first-post'),
61('page-another-post'),
62('page/post/value.html'),
63('page-website/page1');
64
65<?php
66/**
67 * @filename ArticleController.php
68 */
69
70class ArticleController extends CController {
71 public function actionView() {
72 $this->render('view', array(
73 'article' => isset($_GET['article'])?$_GET['article']:'',
74 ));
75 }
76}
77
78<?php
79/**
80 * @filename PageController.php
81 */
82class PageController extends CController {
83 public function actionView() {
84 $this->render('view', array(
85 'page' => isset($_GET['page'])?$_GET['page']:'',
86 ));
87 }
88}
89
90<h1>Article View Test</h1>
91<br />
92<?php
93 if (isset ($article)) echo "article: $article";
94?>
95
96<h1>Page View Test</h1>
97<br />
98<?php
99 if (isset ($page)) echo "page: $page";
100?>
101
102'urlManager'=>array(
103 'urlFormat'=>'path',
104 'class'=>'ext.DbUrlManager.EDbUrlManager',
105 'connectionID'=>'db',
106 'rules'=>array(
107 '<article:[w/.-]+>'=>array(
108 'article/view',
109 'type'=>'db',
110 'fields'=>array(
111 'article'=>array(
112 'table'=>'articles',
113 'field'=>'seoURL'
114 ),
115 ),
116 ),
117
118 '<page:[w/.-]+>'=>array(
119 'page/view',
120 'type'=>'db',
121 'fields'=>array(
122 'page'=>array(
123 'table'=>'pages',
124 'field'=>'seoURL'
125 ),
126 ),
127 ),
128
129 '<controller:w+>/<id:d+>' => '<controller>/view',
130 '<controller:w+>/<action:w+>/<id:d+>' => '<controller>/<action>',
131 '<controller:w+>/<action:w+>' => '<controller>/<action>',
132 ),
133 'showScriptName'=>false,
134),
135
136Options +FollowSymLinks
137IndexIgnore */*
138RewriteEngine on
139
140# if a directory or a file exists, use it directly
141RewriteCond %{REQUEST_FILENAME} !-f
142RewriteCond %{REQUEST_FILENAME} !-d
143
144# otherwise forward it to index.php
145RewriteRule . index.php
146
147dev.yiiblog.com/first-post
148dev.yiiblog.com/page-first-post
149
150'rules' => array(
151 //You should define all the controllers exactly:
152 '<controller:user|archive|office>/<action:w+>' => '<controller>/<action>',
153 //Or more complicated rule:
154 '<lang:(es|it|en)>/(turismo|visita|travel)/<slug:>' => array('visit/page', 'urlSuffix' => '.html'),
155
156 //After that you can process all remaining urls as you want:
157 '<alias:[wd-_/]+>' => array('blog/view', 'urlSuffix' => '.html'),
158 ),
159
160class BlogController extends Controller
161{
162 public function actionView($alias)
163 {
164 echo $alias;
165 }
166}