Cakephp URL Slugs问题

时间:2011-08-02 13:23:22

标签: url cakephp slug

我的网址就像

example.com/deals/by_city/glasgow

我在cities_controller中有一个动作“by_city”,它需要抓住url的“格拉斯哥”部分并做一些事情,我怎么能用cakephp做到这一点?我只想知道如何抓取这部分网址并将其存储在变量中。

所有帮助表示赞赏:)

由于 戴夫

城市控制器

function city_changer($CitySlug = null) {
        $active = $this->City->find('first', array('conditions' => array('City.slug' => $CitySlug)));
        $cities = $this->City->find('all', array('order' => array('City.name', 'City.name ASC')));

        $this->set(compact('active', 'cities'));
    }

City changer.ctp

<h2>Daily Deals from <?php echo $active['City']['name'];?></h2>
<form>
    <select name="file" size="1" onchange="loadPage(this.form.elements[0])" target="_parent._top" onmouseclick="this.focus()">

<?php foreach($cities as $city) {?>        
        <option value="/new_avc/deals/by_city/<?php echo $city['City']['slug'];?>"><?php echo $city['City']['name'];?></option>
<?php }?> 

    </select>
</form>

default.thtml中

<div id="header_wrapper">
    <div id="navigation">
        <div id="logo">
            <h1>Amazing Voucher Codes</h1>
        </div>
        <div id="city_nav">
            <?php echo $this->requestAction('/cities/city_changer/'.$city['City']['slug'], array('return'));?>
        </div>

由于没有链接到default.ctp的数据库在将slug传递给控制器​​时遇到问题:)

2 个答案:

答案 0 :(得分:2)

假设没有自定义路由器规则介入,则URL中的第三个参数(在控制器/操作之后)是传递给操作的第一个参数。

class CitiesController extends AppController {
    function by_city( $cityName ) {
        // $cityName == 'glascow'
    }
}

答案 1 :(得分:2)

您应该只能在$cityname控制器的by_city方法中添加deals参数:这会自动收到城市名称( glasgow 来自上面的网址。)