2013-04-30 70 views
0

創建網址我想獲得的網址如下:Yii的路線,並與多個參數

http://domain.com/post/1/some-titles-here

但我發現了:

http://domain.com/post/1?title=some-titles-here

我正在使用此配置:

'urlFormat' => 'path', 
... 
//'post/<id:\d+>/<title>' => 'post/view/', 
'post/<id:\d+>/<title:\w+>' => 'post/view/', 
'post/<id:\d+>' => 'post/view/', 
... 

然後獲取URL我執行:在此下面

Yii::app()->createAbsoluteUrl('post/view', array('id' => $this->id,'title' => $this->title)); 

IM第三條規則: http://www.yiiframework.com/doc/guide/1.1/en/topics.url#using-named-parameters

+0

嘿,我認爲這是符合這樣的: '交/ '=>'post/view /' 您可以嘗試: 'post//*'=>'post/view /', – Tuan 2013-04-30 20:03:17

+0

您好Jeroes,相同的輸出。 – user2337304 2013-04-30 20:09:19

+0

即時通訊關於此文檔的第三條規則:http://www.yiiframework.com/doc/guide/1.1/en/topics.url#using-named-parameters – user2337304 2013-04-30 20:11:02

回答

1

您使用的是符合題目正則表達式是不正確的: <title:\w+>只會匹配單個單詞,但您的標題也有連字符。

Tuan是正確的;它符合下一條規則。這是因爲URL管理器按照規則工作,直到找到匹配的規則爲止。

使用此規則來代替:

'post/<id:\d+>/<title:([A-Za-z0-9-]+)>' => 'post/view/', 

,它可以匹配字母,數字和連字符的標題。

+0

嗯..這不工作太.. – user2337304 2013-05-01 04:08:27

+0

只需要離開'title'的正則表達式。這將匹配不是'/'的所有內容。最好在你的路線後刪除尾部的'/':''post//'=>'post/view''。 – <span class="text-secondary"> <small> <span>2013-05-01 09:07:28</span> </small> </span> </p> </div> </div> </div> </div> </div> </article> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1038284119" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <article class="board-top-1 padding-top-10"> <div class="post-col vote-info"> <span class="count">0<i class="fa fa-thumbs-up"></i></span> </div> <div class="post-offset"> <div class="answer fmt"> <p>這是爲我工作代碼:</p> <pre><code class="prettyprint-override">'urlManager'=>array( 'urlFormat'=>'path', 'showScriptName' => false, 'rules'=>array( '' => 'site/index', 'article/<id:\d+>/<alias:[-\w]+>' => 'article/view', 'article/cat/<id:\d+>-<alias:[-\w]+>' => 'category/view', </code></pre> <p>結果網址:</p> <ul> <li><a href="http://wowjp.black.dragon/article/2/test-zagolovka" rel="nofollow">http://wowjp.black.dragon/article/2/test-zagolovka</a></li> <li><a href="http://wowjp.black.dragon/article/cat/3-345435" rel="nofollow">http://wowjp.black.dragon/article/cat/3-345435</a></li> </ul> </div> <div class="post-info"> <div class="post-meta row"> <p class="text-secondary col-lg-6"> <span class="source"> <a rel="noopener" target="_blank" href="https://stackoverflow.com/q/27663662">來源</a> </span> </p> <p class="text-secondary col-lg-6"> <span class="float-right date"> <span>2014-12-27 01:41:16</span> </p> <p class="col-12"></p> <p class="col-12"></p></div> </div> </div> </article> </div> <div class="clearfix"> </div> <div class="relative-box"> <div class="relative">相關問題</div> <ul class="relative_list"> <li> 1. <a href="http://hk.uwenku.com/question/p-oiamcpuc-bkp.html" target="_blank" title="一個與ASP.net MVC路線多值路徑參數約束"> 一個與ASP.net MVC路線多值路徑參數約束 </a> </li> <li> 2. <a href="http://hk.uwenku.com/question/p-kvtwfufh-rz.html" target="_blank" title="獲得「爲方法的參數太多應用」路線與多個參數"> 獲得「爲方法的參數太多應用」路線與多個參數 </a> </li> <li> 3. <a href="http://hk.uwenku.com/question/p-qoegrzgn-wv.html" target="_blank" title="多線程參數路過"> 多線程參數路過 </a> </li> <li> 4. <a href="http://hk.uwenku.com/question/p-dckqwnxk-ho.html" target="_blank" title="Sinatra路線參數與RegExp"> Sinatra路線參數與RegExp </a> </li> <li> 5. <a href="http://hk.uwenku.com/question/p-qzbrlgbp-dw.html" target="_blank" title="Laravel路線與n參數"> Laravel路線與n參數 </a> </li> <li> 6. <a href="http://hk.uwenku.com/question/p-hhaqcnja-ok.html" target="_blank" title="路線與參數laravel 5.2"> 路線與參數laravel 5.2 </a> </li> <li> 7. <a href="http://hk.uwenku.com/question/p-awxvikpt-ns.html" target="_blank" title="Yii路由正則表達式多個字作爲參數"> Yii路由正則表達式多個字作爲參數 </a> </li> <li> 8. <a href="http://hk.uwenku.com/question/p-kcjhmjys-wb.html" target="_blank" title="ASP.NET MVC路由:404與多個參數"> ASP.NET MVC路由:404與多個參數 </a> </li> <li> 9. <a href="http://hk.uwenku.com/question/p-qzfaspfu-ye.html" target="_blank" title="葡萄中的多重路線參數"> 葡萄中的多重路線參數 </a> </li> <li> 10. <a href="http://hk.uwenku.com/question/p-zrzvxrgm-tv.html" target="_blank" title="合併多條線路與條件"> 合併多條線路與條件 </a> </li> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-6208739752673518" data-ad-slot="4606349252"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <li> 11. <a href="http://hk.uwenku.com/question/p-nhapksgr-ks.html" target="_blank" title="具有多個參數,可選參數和漂亮參數的路線"> 具有多個參數,可選參數和漂亮參數的路線 </a> </li> <li> 12. <a href="http://hk.uwenku.com/question/p-nmttypli-bhp.html" target="_blank" title="帶參數的Yii url路由"> 帶參數的Yii url路由 </a> </li> <li> 13. <a href="http://hk.uwenku.com/question/p-bpifnwxu-boe.html" target="_blank" title="創建multible參數Yii的URL路徑"> 創建multible參數Yii的URL路徑 </a> </li> <li> 14. <a href="http://hk.uwenku.com/question/p-phgnscro-tt.html" target="_blank" title="路線與多個文件的NodeJS"> 路線與多個文件的NodeJS </a> </li> <li> 15. <a href="http://hk.uwenku.com/question/p-kgmvzfee-baa.html" target="_blank" title="路線與多個標識的(Laravel 5.2)"> 路線與多個標識的(Laravel 5.2) </a> </li> <li> 16. <a href="http://hk.uwenku.com/question/p-cwrrhaez-ya.html" target="_blank" title="Yii的兩個參數"> Yii的兩個參數 </a> </li> <li> 17. <a href="http://hk.uwenku.com/question/p-bxqajdxw-ow.html" target="_blank" title="多值GET參數 - 線路生成"> 多值GET參數 - 線路生成 </a> </li> <li> 18. <a href="http://hk.uwenku.com/question/p-qyanufqz-ng.html" target="_blank" title="與聯合URL參數地圖路線"> 與聯合URL參數地圖路線 </a> </li> <li> 19. <a href="http://hk.uwenku.com/question/p-nnozllnc-ut.html" target="_blank" title="MVC圖路線與參數票ID"> MVC圖路線與參數票ID </a> </li> <li> 20. <a href="http://hk.uwenku.com/question/p-pvrliyji-nq.html" target="_blank" title="aurelia兒童路線與「設置」參數"> aurelia兒童路線與「設置」參數 </a> </li> <li> 21. <a href="http://hk.uwenku.com/question/p-bkclddae-bac.html" target="_blank" title="ExpressJS路由參數與斜線"> ExpressJS路由參數與斜線 </a> </li> <li> 22. <a href="http://hk.uwenku.com/question/p-wrbuzkfe-bhz.html" target="_blank" title="路線Globbing與參數包含斜槓"> 路線Globbing與參數包含斜槓 </a> </li> <li> 23. <a href="http://hk.uwenku.com/question/p-blkxkwpx-re.html" target="_blank" title="P線程:線程調用函數與多個參數"> P線程:線程調用函數與多個參數 </a> </li> <li> 24. <a href="http://hk.uwenku.com/question/p-ajzudpxq-ng.html" target="_blank" title="多個參數路由"> 多個參數路由 </a> </li> <li> 25. <a href="http://hk.uwenku.com/question/p-gebvxxuw-zw.html" target="_blank" title="參數在路線"> 參數在路線 </a> </li> <li> 26. <a href="http://hk.uwenku.com/question/p-vpaphrbr-ec.html" target="_blank" title="與多個參數"> 與多個參數 </a> </li> <li> 27. <a href="http://hk.uwenku.com/question/p-ylmozzuu-bcp.html" target="_blank" title="與多個參數"> 與多個參數 </a> </li> <li> 28. <a href="http://hk.uwenku.com/question/p-rscbgked-zn.html" target="_blank" title="的WebAPI路由與多個可選參數的函數"> 的WebAPI路由與多個可選參數的函數 </a> </li> <li> 29. <a href="http://hk.uwenku.com/question/p-xrcnpzup-gb.html" target="_blank" title="笨 - 航線多個參數"> 笨 - 航線多個參數 </a> </li> <li> 30. <a href="http://hk.uwenku.com/question/p-zutaxwrv-bkh.html" target="_blank" title="如何綁定多個相關參數在一個路線laravel"> 如何綁定多個相關參數在一個路線laravel </a> </li> </ul> </div> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1575177025"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="padding-top-10"></div> </div> </div> <script type="text/javascript" src="http://img.uwenku.com/uwenku/script/side.js?t=1644592048261"></script> <script type="text/javascript" src="http://img.uwenku.com/uwenku/plugin/highlight/highlight.pack.js"></script> <link href="http://img.uwenku.com/uwenku/plugin/highlight/styles/docco.css" media="screen" rel="stylesheet" type="text/css" /> <script type="text/javascript"> $('pre').each(function(i, e) { hljs.highlightBlock(e, "<span class='indent'> </span>", false) }); </script> <div class="col-lg-3 col-md-4 col-sm-5"> <div id="rightTop"> <div class="row"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="5415218910" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="row sidebar panel panel-default"> <div class="panel-heading font-bold"> 最新問題 </div> <div class="m-b-sm m-t-sm clearfix"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-tvgmrvpy-hb.html" target="_blank" title="Spyder 3.1.4 - 操作系統升級後無法配置Spyder"> Spyder 3.1.4 - 操作系統升級後無法配置Spyder </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-nrnjrlra-kr.html" target="_blank" title="能從應用程序的BeginRequest在第一次請求使用RewritePath"> 能從應用程序的BeginRequest在第一次請求使用RewritePath </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-xyxztndm-hw.html" target="_blank" title="如何使用帶空格的HTML Git Diff?"> 如何使用帶空格的HTML Git Diff? </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-fsgdmwpl-eb.html" target="_blank" title="如何使用setActiveItem進行卡片佈局以基於radiobuttton選擇顯示面板?"> 如何使用setActiveItem進行卡片佈局以基於radiobuttton選擇顯示面板? </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-zcmykspd-dh.html" target="_blank" title="爲什麼如果(a!= 65)沒有執行?"> 爲什麼如果(a!= 65)沒有執行? </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-vdmsgred-ex.html" target="_blank" title="字符串中最後2個括號之間的所有大寫字母的正則表達式"> 字符串中最後2個括號之間的所有大寫字母的正則表達式 </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-fricngen-z.html" target="_blank" title="由不同的解決方案調用的一個共享項目中的JS文件"> 由不同的解決方案調用的一個共享項目中的JS文件 </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-aiwiznwb-d.html" target="_blank" title="Java 8 DateTimeFormatterBuilder()。appendOptional不工作"> Java 8 DateTimeFormatterBuilder()。appendOptional不工作 </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-eoqvbwzw-cc.html" target="_blank" title="傳數據從小孩到在母體陣營節目類型錯誤"> 傳數據從小孩到在母體陣營節目類型錯誤 </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-pjpbavcz-bmd.html" target="_blank" title="谷歌附近的通知分析"> 谷歌附近的通知分析 </a> </li> </ul> </div> </div> </div> <p class="article-nav-bar"></p> <div class="row sidebar article-nav"> <div class="row box_white visible-sm visible-md visible-lg margin-zero"> <div class="top"> <h3 class="title"><i class="glyphicon glyphicon-th-list"></i> 相關問題</h3> </div> <div class="article-relative-content"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-oiamcpuc-bkp.html" target="_blank" title="一個與ASP.net MVC路線多值路徑參數約束"> 一個與ASP.net MVC路線多值路徑參數約束 </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-kvtwfufh-rz.html" target="_blank" title="獲得「爲方法的參數太多應用」路線與多個參數"> 獲得「爲方法的參數太多應用」路線與多個參數 </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-qoegrzgn-wv.html" target="_blank" title="多線程參數路過"> 多線程參數路過 </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-dckqwnxk-ho.html" target="_blank" title="Sinatra路線參數與RegExp"> Sinatra路線參數與RegExp </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-qzbrlgbp-dw.html" target="_blank" title="Laravel路線與n參數"> Laravel路線與n參數 </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-hhaqcnja-ok.html" target="_blank" title="路線與參數laravel 5.2"> 路線與參數laravel 5.2 </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-awxvikpt-ns.html" target="_blank" title="Yii路由正則表達式多個字作爲參數"> Yii路由正則表達式多個字作爲參數 </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-kcjhmjys-wb.html" target="_blank" title="ASP.NET MVC路由:404與多個參數"> ASP.NET MVC路由:404與多個參數 </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-qzfaspfu-ye.html" target="_blank" title="葡萄中的多重路線參數"> 葡萄中的多重路線參數 </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-zrzvxrgm-tv.html" target="_blank" title="合併多條線路與條件"> 合併多條線路與條件 </a> </li> </ul> </div> </div> </div> </div> </div> </div> </div><!-- wrap end--> <!-- footer --> <footer id="footer"> <div class="bg-simple lt"> <div class="container"> <div class="row padder-v m-t"> <div class="col-xs-8"> <ul class="list-inline"> <li><a href="http://hk.uwenku.com/contact">聯系我們</a></li> <li>© 2020 HK.UWENKU.COM</li> <li><a target="_blank" href="https://beian.miit.gov.cn/">沪ICP备13005482号-4</a></li> <li><script type="text/javascript" src="https://v1.cnzz.com/z_stat.php?id=1280101193&web_id=1280101193"></script></li> <li><a href="http://www.uwenku.com/" target="_blank" title="优文库">简体中文</a></li> <li><a href="http://hk.uwenku.com/" target="_blank" title="優文庫">繁體中文</a></li> <li><a href="http://ru.uwenku.com/" target="_blank" title="поле вопросов и ответов">Русский</a></li> <li><a href="http://de.uwenku.com/" target="_blank" title="Frage - und - antwort - Park">Deutsch</a></li> <li><a href="http://es.uwenku.com/" target="_blank" title="Preguntas y respuestas">Español</a></li> <li><a href="http://hi.uwenku.com/" target="_blank" title="कार्यक्रम प्रश्न और उत्तर पार्क">हिन्दी</a></li> <li><a href="http://it.uwenku.com/" target="_blank" title="IL Programma di chiedere Park">Italiano</a></li> <li><a href="http://ja.uwenku.com/" target="_blank" title="プログラム問答園区">日本語</a></li> <li><a href="http://ko.uwenku.com/" target="_blank" title="프로그램 문답 단지">한국어</a></li> <li><a href="http://pl.uwenku.com/" target="_blank" title="program o park">Polski</a></li> <li><a href="http://tr.uwenku.com/" target="_blank" title="Program soru ve cevap parkı">Türkçe</a></li> <li><a href="http://vi.uwenku.com/" target="_blank" title="Đáp ứng viên">Tiếng Việt</a></li> <li><a href="http://fr.uwenku.com/" target="_blank" title="Programme interrogation Park">Française</a></li> </ul> </div> </div> </div> </div> </div> </footer> <!-- / footer --> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?f78a970f17b19a79fc477a3378096f29"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>