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

Yii 1.x URL 管理器 (CUrlManager) 完整配置與路由美化指南

     編輯:PHP 相關知識

核心配置:path 模式 + 隱藏 index.php

'components'=>[
    'urlManager'=>[
        'urlFormat'=>'path',           // 路徑格式:/controller/action/param/value
        'showScriptName'=>false,       // 隱藏 index.php
        'urlSuffix'=>'.html',          // 可選:偽靜態後綴
        'caseSensitive'=>false,        // 不區分大小寫
        'rules'=>[
            'posts'=>'post/list',
            'post/'=>'post/view',
            'post//'=>'post/view',
            '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
        ],
    ],
],</code></pre>

<h2>Apache .htaccess(與 index.php 同級目錄)</h2>
<pre class="code"><code>RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php</code></pre>
<p>確保 Apache 載入 <code>mod_rewrite</code> 並允許 <code>AllowOverride All</code>。</p>

<h2>規則寫法詳解</h2>
<table border="1" cellpadding="6" cellspacing="0">
<tr><th>規則</th><th>匹配 URL</th><th>路由到</th></tr>
<tr><td><code>'posts'=>'post/list'</code></td><td><code>/posts.html</code></td><td><code>PostController::actionList()</code></td></tr>
<tr><td><code>'post/<id:\d+>'=>'post/view'</code></td><td><code>/post/123.html</code></td><td><code>actionView($id=123)</code></td></tr>
<tr><td><code>'post/<id:\d+>/<title>'=>'post/view'</code></td><td><code>/post/123/hello-world.html</code></td><td><code>actionView($id=123, $title='hello-world')</code></td></tr>
<tr><td><code>'<controller>/<action>'=>'<controller>/<action>'</code></td><td><code>/user/login.html</code></td><td><code>UserController::actionLogin()</code></td></tr>
</table>

<h2>進階技巧</h2>
<ul>
<li><strong>參數預設值</strong>:<code>'tos'=>['site/page', 'defaultParams'=>['alias'=>'terms']]</code> 訪問 <code>/tos.html</code> 等同 <code>/site/page/alias/terms.html</code>。</li>
<li><strong>不同後綴</strong>:<code>'post/<id>'=>['post/view', 'urlSuffix'=>'.json']</code> 單獨指定 JSON 後綴。</li>
<li><strong>主機名路由</strong>:<code>'https://<user:\w+>.example.com/<action>' => '<action>/host'</code> 支持二級域名路由。</li>
<li><strong>控制器獲取參數</strong>:<code>actionView($id)</code> 自動綁定,或 <code>Yii::app()->request->getQuery('id')</code>。</li>
</ul>

<h2>性能提示</h2>
<p>規則過多會降低解析速度(線性匹配)。高流量站點建議:</p>
<ol>
<li>盡量合併通用規則,減少條目數。</li>
<li>將常用路由前置。</li>
<li>考慮緩存解析結果或改用 Nginx rewrite 直接轉發。</li>
</ol>
							<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/gyphp/139777.html' >Yii 1.x 去除表單必填項標籤後的紅色星號(*)</a> <li>下一篇文章: <a class='LinkNextArticle' href='https://code.yunyiwangluo.com/bianchen/gyphp/139779.html'  >Yii 1.x 博客系統:文章詳情頁嵌入評論表單完整實現</a> 
						</ol>
					</div>
				</div>
				<div class="qirnxbd bg1 mt20">
					<div class="jdbsbav">
						<a href='https://code.yunyiwangluo.com/bianchen/gyphp/' target="_blank">PHP 相關知識</a>
						<div class="jdbsbav_hx"></div>
					</div>
					<div class="qirnxbd_rbx">
						
						<div class="oiendx">
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/154557.html' target="_blank"><img  src='https://code.yunyiwangluo.com/bianchen/UploadFiles_4619/201701/2017011717400659.jpg'  width='100' height='70' alt='微信自定義菜單接口全攻略:創建、查詢、刪除與按鈕類型詳解'  border='0'></a>
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/154557.html' target="_blank"><strong>微信自定義菜單接口全攻略:創建、查詢、刪除與按鈕類型詳解</strong></a>
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/154557.html' target="_blank"><p>簡介
開發者獲取憑證(access_token)後,即可對公</p></a>			
						</div>
						
						<div class="oiendx">
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/40416.html' target="_blank"><img  src='https://code.yunyiwangluo.com/bianchen/UploadFiles_4619/201701/2017010910070766.png'  width='100' height='70' alt='使用 Docker 搭建 PHP 開發環境完整指南'  border='0'></a>
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/40416.html' target="_blank"><strong>使用 Docker 搭建 PHP 開發環境完整指南</strong></a>
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/40416.html' target="_blank"><p>Docker 以其輕量級、可移植的特性,已成為現代 PHP </p></a>			
						</div>
						
						<div class="oiendx">
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/144762.html' target="_blank"><img  src='https://code.yunyiwangluo.com/bianchen/UploadFiles_4619/201701/2017011710334372_S.jpg'  width='100' height='70' alt='PHP 5.4 Windows 7 下 Memcached 分布式緩存安裝配置全記錄'  border='0'></a>
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/144762.html' target="_blank"><strong>PHP 5.4 Windows 7 下 Memcached 分布式緩存安裝配置全記錄</strong></a>
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/144762.html' target="_blank"><p>PHP 5.4 Windows 7 下 Memcached </p></a>			
						</div>
						
						<div class="oiendx">
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/142540.html' target="_blank"><img  src='https://code.yunyiwangluo.com/bianchen/UploadFiles_4619/201701/2017011709252587_S.jpg'  width='100' height='70' alt='WAMP 整合開發環境安裝圖文教程:Windows 下 Apache + MySQL + PHP 一鍵部署'  border='0'></a>
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/142540.html' target="_blank"><strong>WAMP 整合開發環境安裝圖文教程:Windows 下 Apache + MySQL + PHP 一鍵部署</strong></a>
							<a href='https://code.yunyiwangluo.com/bianchen/gyphp/142540.html' target="_blank"><p>WAMP Server 安裝完整圖文教程:Windows 上</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/gyphp/153514.html' target="_blank">PHP 物件序列化與反序列化:__sleep、__wakeup、__serialize 完整指南</a>
						
						<a href='https://code.yunyiwangluo.com/bianchen/gyphp/138403.html' target="_blank">PHP 比較與邏輯運算深度解析(圖文版):型別轉換規則、三元運算左結合與 switch 鬆散比較</a>
						
						<a href='https://code.yunyiwangluo.com/bianchen/gyphp/159779.html' target="_blank">PHP 生成靜態 HTML 頁面實戰:模板渲染、分頁生成、目錄管理完整方案</a>
						
						<a href='https://code.yunyiwangluo.com/bianchen/gyphp/149363.html' target="_blank">PHP 安全過濾類實現:XSS 防護與 SQL 注入防禦雙重過濾</a>
						
						<a href='https://code.yunyiwangluo.com/bianchen/gyphp/149256.html' target="_blank">Memcached 快取清除實戰指南:從 flush_all 到命名空間模式</a>
						
						<a href='https://code.yunyiwangluo.com/bianchen/gyphp/144084.html' target="_blank">ThinkPHP 表單自動驗證運用示例(精簡版)</a>
						
						<a href='https://code.yunyiwangluo.com/bianchen/gyphp/148912.html' target="_blank">Windows下MySQL服務安裝卸載腳本與啟動故障終極排查</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>