首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

Yii URL 管理:urlManager 規則配置與隱藏 index.php 完整教學

     編輯:PHP 綜合知識

Yii urlManager 核心屬性與規則寫法

CUrlManager 讓 URL 更具可讀性與 SEO 友善。本文詳細說明常用屬性、規則範例與隱藏入口腳本步驟。

關鍵屬性

  • urlSuffix:URL 後綴(如 .html),預設無。
  • caseSensitive:是否大小寫敏感,預設 true。
  • defaultParams:預設 GET 參數,解析請求時自動注入 $_GET。
  • matchValue:建立 URL 時是否檢查參數值符合子模式,開啟會影響效能。

規則範例

'rules' => array(
    'posts' => 'post/list',
    'post/' => 'post/read',
    'post//' => 'post/read',
)</pre>
<ul>
<li><code>$this->createUrl('post/list')</code> → <code>/index.php/posts</code></li>
<li><code>$this->createUrl('post/read', array('id'=>100))</code> → <code>/index.php/post/100</code></li>
<li><code>$this->createUrl('post/read', array('year'=>2008,'title'=>'a sample post'))</code> → <code>/index.php/post/2008/a%20sample%20post</code></li>
</ul>

<h3>進階規則:參數綁定與後綴</h3>
<pre class="brush:php;">'urlManager' => array(
    'urlFormat' => 'path',
    'showScriptName' => false,
    'urlSuffix' => '.html',
    'rules' => array(
        'posts' => 'post/list',
        'post/<id:\d+>' => array('post/show', 'urlSuffix' => '.html'),
        'post/<id:\d+>/<mid:\w+>' => array('post/view', 'urlSuffix' => '.xml'),
    ),
)</pre>
<ul>
<li><code>createUrl('post/show', array('id'=>998, 'name'=>'123'))</code> → <code>/post/998.html?name=123</code></li>
<li><code>createUrl('post/view', array('id'=>998, 'mid'=>'tody'))</code> → <code>/post/998/tody.xml</code></li>
</ul>

<h3>隱藏 index.php</h3>
<ol>
<li><code>showScriptName => false</code></li>
<li>專案根目錄建立 <code>.htaccess</code>:</li>
</ol>
<pre class="brush:xhtml;">RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php</pre>
<ol start="3">
<li>Apache 啟用 <code>mod_rewrite</code>。</li>
</ol>

<h3>控制器取得參數</h3>
<pre class="brush:php;">public function actionUpdate($id) { ... } // 自動綁定 $_GET['id']
// 或
$id = Yii::app()->request->getQuery('id');</pre>

<h3>效能提醒</h3>
<p>規則過多會降低解析效能,高流量網站應精簡規則數量。</p>
							<div class="qcxs_ie_he"></div>
						</div>
						<div class="akevx_own"> <script language='javascript' src='/ad/p.js'></script></div>
						<div class="akevx_mj"> </div>	
						<div class="qcxs_ie_he"></div>
						
						<ol class="qmsbvx_hsb ovv_hex">
							<li>上一篇文章: <a class='LinkPrevArticle' href='https://code.yunyiwangluo.com/bianchen/phpzh/58507.html' >Yii 部落格系統:文章詳情頁整合評論表單與提交處理</a> <li>下一篇文章: <a class='LinkNextArticle' href='https://code.yunyiwangluo.com/bianchen/phpzh/58509.html'  >Yii 表單標籤客製化:移除或替換必填欄位星號</a> 
						</ol>
					</div>
				</div>
				<div class="qirnxbd bg1 mt20">
					<div class="jdbsbav">
						<a href='https://code.yunyiwangluo.com/bianchen/phpzh/' target="_blank">PHP 綜合知識</a>
						<div class="jdbsbav_hx"></div>
					</div>
					<div class="qirnxbd_rbx">
						
						<div class="oiendx">
							<a href='https://code.yunyiwangluo.com/bianchen/phpzh/16785.html' target="_blank"><img  src='https://code.yunyiwangluo.com/bianchen/UploadFiles_4619/201701/2017010509524880_S.jpg'  width='100' height='70' alt='Zend Framework 自定義分頁類實現與擴展實戰'  border='0'></a>
							<a href='https://code.yunyiwangluo.com/bianchen/phpzh/16785.html' target="_blank"><strong>Zend Framework 自定義分頁類實現與擴展實戰</strong></a>
							<a href='https://code.yunyiwangluo.com/bianchen/phpzh/16785.html' target="_blank"><p>方案來源
參考社區方案改進擴展:原文鏈接



實現步驟
在</p></a>			
						</div>
						
						<div class="oiendx">
							<a href='https://code.yunyiwangluo.com/bianchen/phpzh/59451.html' target="_blank"><img  src='https://code.yunyiwangluo.com/bianchen/UploadFiles_4619/201701/2017011012423836.png'  width='100' height='70' alt='ThinkPHP 2.2 文檔未提及的 5 大實戰坑位與解決方案'  border='0'></a>
							<a href='https://code.yunyiwangluo.com/bianchen/phpzh/59451.html' target="_blank"><strong>ThinkPHP 2.2 文檔未提及的 5 大實戰坑位與解決方案</strong></a>
							<a href='https://code.yunyiwangluo.com/bianchen/phpzh/59451.html' target="_blank"><p>前言
ThinkPHP 2.2 官方文檔較精簡,實際開發中常</p></a>			
						</div>
						
						<div class="oiendx">
							<a href='https://code.yunyiwangluo.com/bianchen/phpzh/67193.html' target="_blank"><img  src='https://code.yunyiwangluo.com/bianchen/UploadFiles_4619/201701/2017011020141624_S.jpg'  width='100' height='70' alt='phpMyAdmin 安裝與設定圖文教學(更新版)'  border='0'></a>
							<a href='https://code.yunyiwangluo.com/bianchen/phpzh/67193.html' target="_blank"><strong>phpMyAdmin 安裝與設定圖文教學(更新版)</strong></a>
							<a href='https://code.yunyiwangluo.com/bianchen/phpzh/67193.html' target="_blank"><p>phpMyAdmin 簡介
phpMyAdmin 是以 PH</p></a>			
						</div>
						
						<div class="oiendx">
							<a href='https://code.yunyiwangluo.com/bianchen/phpzh/59621.html' target="_blank"><img  src='https://code.yunyiwangluo.com/bianchen/UploadFiles_4619/201701/2017011012514337.png'  width='100' height='70' alt='PHP 中 Unicode 與 UTF-8 編碼深度解析:轉換原理與實戰代碼'  border='0'></a>
							<a href='https://code.yunyiwangluo.com/bianchen/phpzh/59621.html' target="_blank"><strong>PHP 中 Unicode 與 UTF-8 編碼深度解析:轉換原理與實戰代碼</strong></a>
							<a href='https://code.yunyiwangluo.com/bianchen/phpzh/59621.html' target="_blank"><p>Unicode 與 UTF-8 的核心差異

Unicode</p></a>			
						</div>
						
						<div class="qcxs_ie_he"></div>
					</div>
				</div>
			</div>
			<div class="eccsgxsg mt20">
				
				<div class="akevx_oin"> </div>	
				<div class="nenxnxb bg1 mt20">
					<div class="enxvsfgg">
						<div class="enxvsfgg_1"></div>
					</div>
					<div class="pdirbvx lbhevs ovv_hex">
						
						<a href='https://code.yunyiwangluo.com/bianchen/phpzh/62973.html' target="_blank">PHP 二維陣列指定鍵值排序通用函數(支援升降序、多鍵擴展)</a>
						
						<a href='https://code.yunyiwangluo.com/bianchen/phpzh/16662.html' target="_blank">純 PHP 實現單點登入 (SSO) 系統:基於 Discuz 加密演算法的輕量級方案</a>
						
						<a href='https://code.yunyiwangluo.com/bianchen/phpzh/56412.html' target="_blank">Fleaphp 框架核心資料庫操作類 FLEA_Db_TableDataGateway 完整方法參考與實戰範例</a>
						
						<a href='https://code.yunyiwangluo.com/bianchen/phpzh/59003.html' target="_blank">ASP 經典教學:Command 物件完整操作指南與員工管理系統實作</a>
						
						<a href='https://code.yunyiwangluo.com/bianchen/phpzh/64290.html' target="_blank">PHP 圖片上傳重命名 6 大方案深度對比:從 GUID 到 Base64 編碼最終優化</a>
						
						<a href='https://code.yunyiwangluo.com/bianchen/phpzh/57564.html' target="_blank">Yii2 分頁元件完整指南:基本用法、屬性客製化與元件擴展實戰</a>
						
						<a href='https://code.yunyiwangluo.com/bianchen/phpzh/57197.html' target="_blank">PHP + AJAX 即時驗證碼驗證:跨域 Session 共享問題與解決方案</a>
						
					</div>
				</div>
			
				<div class="eqibneb bg1 mt20">
					<div class="lkebxsg">欄目導航</div>
					<div class="eqibneb_1 ovv_hex">
						<li><a class='childclass' href='https://code.yunyiwangluo.com/bianchen/phpjc/' target="_self">PHP 基礎</a></li>
<li><a class='childclass' href='https://code.yunyiwangluo.com/bianchen/phpzh/' target="_self">PHP 綜合知識</a></li>
<li><a class='childclass' href='https://code.yunyiwangluo.com/bianchen/phprm/' target="_self">PHP 入門</a></li>
<li><a class='childclass' href='https://code.yunyiwangluo.com/bianchen/gyphp/' target="_self">PHP 相關知識</a></li>
					</div>
					
				</div>
			</div>
			<div class="qcxs_ie_he"></div>
		</div>
	</div>
		<div class="qcxs_ie_he"></div>
	<div class="ebrxbsbf">Copyright © <a href="https://code.yunyiwangluo.com/" target="_blank" title="程式碼之家">程式碼之家</a> All Rights Reserved   </div>


</body>
</html>