我將一步一步解釋如何讓這個工作。
第1步 - 創建Yii的Web應用程序
瀏覽到您的控制檯您Yii框架路徑和創建新的Web應用程序。
cd c:\zeus\yii-1.1.10.r3566\framework
yiic webapp c:\zeus\www\yiiblog
其中c:\宙斯\警予,1.1.10.r3566 \框架是我的路徑Yii的PHP框架和c:\宙斯\ WWW \ yiiblog是在我的情況我在控制檯中使用此路徑我的Yii web應用程序的測試文件夾
撲進2 - 假冒我的域名dev.yiiblog.com
轉到C:通過加入這一行\ Windows \ System32下\ drivers \ etc下和編輯您的主機文件:
127.0.0.1 dev.yiiblog.com
第3步 - 改變ap ache httpd.conf文件
<VirtualHost *:80>
DocumentRoot "c:/zeus/www/yiiblog"
ServerName dev.yiiblog.com
ErrorLog "logs/dev.yiiblog.com-error.log"
CustomLog "logs/dev.yiiblog.com-access.log" common
</VirtualHost>
並重新啓動apache服務。我在Windows控制檯中使用:
net stop apache
net start apache
在我的Apache 2的服務被命名爲「阿帕奇」而不是「APACHE2.2」之類的默認值。
第4步 - 創建一個數據庫和配置數據庫連接到的Yii
我創建了一個數據庫yiitest和用戶yiitest。然後我打開位於ad/protected/config/main中的Yii配置文件。PHP和編輯的連接到MySQL:
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=yiitest',
'emulatePrepare' => true,
'username' => 'yiitest',
'password' => 'password',
'charset' => 'utf8',
),
第5步 - 下載dburlmanager Yii的擴展
轉到Yii的dburlmanager,下載Yii的dburlmanager擴展http://www.yiiframework.com/extension/dburlmanager/並將其解壓到你的/保護/ Extensions文件夾
第6步 - 創建MySQL數據庫表,並添加虛擬數據
CREATE TABLE IF NOT EXISTS `articles` (
`seoURL` varchar(100) NOT NULL
) ENGINE=InnoDB;
INSERT INTO `articles` (`seoURL`) VALUES
('first-post'),
('another-post'),
('post/value'),
('website/page1');
CREATE TABLE IF NOT EXISTS `pages` (
`seoURL` varchar(100) NOT NULL
) ENGINE=InnoDB;
INSERT INTO `pages` (`seoURL`) VALUES
('page-first-post'),
('page-another-post'),
('page/post/value.html'),
('page-website/page1');
第7步 - 創建自定義的Yii控制器小號
下創建/保護/控制器文件夾兩個PHP文件名爲ArticleController.php和PageController.php:
ArticleController.php內容:
<?php
/**
* @filename ArticleController.php
*/
class ArticleController extends CController {
public function actionView() {
$this->render('view', array(
'article' => isset($_GET['article'])?$_GET['article']:'',
));
}
}
PageController.php內容:
<?php
/**
* @filename PageController.php
*/
class PageController extends CController {
public function actionView() {
$this->render('view', array(
'page' => isset($_GET['page'])?$_GET['page']:'',
));
}
}
第8步 - 創建您自定義的Yii視圖
上面創建的路徑/protected/views/article/view.php和/protected/views/page/view.php對應於那些控制器視圖文件:
文章觀點內容:
<h1>Article View Test</h1>
<br />
<?php
if (isset ($article)) echo "article: $article";
?>
頁面視圖內容:
<h1>Page View Test</h1>
<br />
<?php
if (isset ($page)) echo "page: $page";
?>
第9步 - 添加自定義的Yii URL規則
再次打開你的main.php Yii的配置文件,您urlManager設置類似於:
'urlManager'=>array(
'urlFormat'=>'path',
'class'=>'ext.DbUrlManager.EDbUrlManager',
'connectionID'=>'db',
'rules'=>array(
'<article:[\w\/.-]+>'=>array(
'article/view',
'type'=>'db',
'fields'=>array(
'article'=>array(
'table'=>'articles',
'field'=>'seoURL'
),
),
),
'<page:[\w\/.-]+>'=>array(
'page/view',
'type'=>'db',
'fields'=>array(
'page'=>array(
'table'=>'pages',
'field'=>'seoURL'
),
),
),
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
'showScriptName'=>false,
),
第10步 - 創建.htaccess文件
您的Web應用程序根目錄下創建一個.htaccess文件和ETID其內容:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
第11步 - 測試你的SEO友好的URL
dev.yiiblog.com/first-post
dev.yiiblog.com/page-first-post
等
玩得開心創造出色的博客或其他網絡應用程序,具有完整的網址管理權力。
當前的答案似乎相當有用(特別是橡子的),什麼是給你404的網址? –