久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

CakePHP - 如何使用 slug 進行反向路由?

CakePHP - How to do reverse routing with slug?(CakePHP - 如何使用 slug 進行反向路由?)
本文介紹了CakePHP - 如何使用 slug 進行反向路由?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我使用的是 CakePHP 1.3.我有一個產(chǎn)品模型.在 DB 表中還有 idslug 字段.

I am using CakePHP 1.3. I have a Product model. on the DB table among others there are id and slug fields.

如果我有一個 id:37slug:My-Product-Title 的產(chǎn)品,我希望產(chǎn)品的 URL 是:

If I have a product that is id:37 and slug:My-Product-Title I want the URL for the product to be:

products/37/My-Product-Title

products/37/My-Product-Title

代替標(biāo)準(zhǔn):

產(chǎn)品/視圖/37

我創(chuàng)建了一條如下所示的路線:

I created a route that looks like this:

Router::connect(
    '/products/:id/:slug',
    array('controller' => 'products', 'action' => 'view'),
    array('pass' => array('id'), 'id' => '[0-9]+')
);

現(xiàn)在我可以轉(zhuǎn)到 http://server/products/37/My-Product-Title,它會將我?guī)У秸_的位置.

Now I can go to http://server/products/37/My-Product-Title and it takes me to the right place.

但是如何獲得反向路由以在 $HtmlHelper->link 中自動構(gòu)建正確的 URL?

But How do I get reverse routing to automatically build the correct URL in $HtmlHelper->link?

當(dāng)我使用時:

echo $html->link(
    'Product 37', 
    array('controller'=>'products', 'action' => 'view', 37)
);

它仍然輸出標(biāo)準(zhǔn)的products/view/37 url.

It still outputs the standard products/view/37 url.

推薦答案

我認為不可能自動完成.幫手只是一個幫手",他根據(jù)給定的參數(shù)構(gòu)建鏈接.

I don't believe that it's possible to be done auto-magically. The helper is just an "helper" who builds the link from the given parameters.

所以最簡單的方法是在鏈接中添加另一個參數(shù),如下所示:

So the easiest method is to add another parameter in your link like so:

echo $html->link(
    'Product 37', 
    array('controller'=>'products', 'action' => 'view', 37, $slug)
);

其中 $slug 是來自 slug 字段的數(shù)據(jù).

where the $slug is the data from the slug field.

可能可以實現(xiàn)您的想法,但是您需要非常嚴重地打破 MVC 模式 :)

Probably it could be done your idea, but you need to break the MVC pattern very badly :)

再次閱讀您的問題,我明白了.看看應(yīng)該怎么做:

Reading your question again I understood it well. See how should be done:

在您的 router.php 中添加以下規(guī)則:

in your router.php add the following rule:

Router::connect(
    '/product/*',
    array('controller' => 'products', 'action' => 'view')
);

請注意它是/product/* 而不是/products/*

Please note that it's /product/* rather than /products/*

你的鏈接應(yīng)該是這樣的:

Your link should be done like this:

echo $html->link(
    'Product 37', 
    array('controller'=>'products', 'action' => 'view', 37, 'my-product-title')
);

鏈接看起來像:

http://yourdomain.com/product/37/my-product-title

對我來說,你的建議是不好的做法.此外,我認為從 SEO 的角度來看總是重定向用戶并不好.

For me doing your suggestion is bad practice. Also I don't think it's good from SEO point of view redirecting always the user.

這篇關(guān)于CakePHP - 如何使用 slug 進行反向路由?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Action View Helper in Zend - Work around?(Zend 中的動作視圖助手 - 解決方法?)
Is this a good way to match URI to class/method in PHP for MVC(這是將 URI 與 PHP 中用于 MVC 的類/方法匹配的好方法嗎)
Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(視圖),以便我的應(yīng)用程序中的所有視圖都可以訪問?) - IT屋-程序員軟件開發(fā)技術(shù)
Having a single entry point to a website. Bad? Good? Non-issue?(有一個網(wǎng)站的單一入口點.壞的?好的?沒問題?)
Is MVC + Service Layer common in zend or PHP?(MVC + 服務(wù)層在 Zend 或 PHP 中常見嗎?)
Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)
主站蜘蛛池模板: 亚洲午夜精品一区二区三区他趣 | 久久久久国产精品一区二区 | 日韩成人在线视频 | 91视频在线网站 | 国产高清精品一区二区三区 | 久久久久久黄 | 91久久国产综合久久 | 国产精品久久久久久久久久久免费看 | 亚洲一区二区高清 | 日韩免费在线 | 亚洲一区免费视频 | 色综合久久久久 | 精品中文视频 | 日本一区二区高清不卡 | 亚洲一区二区三区观看 | 香蕉一区 | 毛片在线免费 | 久久久久国产 | 色综合久久天天综合网 | 成人av网站在线观看 | 天天看夜夜 | www.日日操 | 一区二区三区在线免费观看 | 国产一区二区三区四区 | 国产成人99久久亚洲综合精品 | 一级a爱片性色毛片免费 | 国产一级淫片免费视频 | 中文字幕欧美在线观看 | 亚洲精品欧美 | 欧美一区二区三区在线观看 | 伊人av在线播放 | av网站免费 | 日本久久网站 | 蜜桃五月天 | 国产成人精品一区二三区在线观看 | 黄色av大片 | 日韩精品成人 | 伊人狠狠操 | 久久久久久久亚洲精品 | 亚洲三区视频 | 国产在线精品一区二区 |