CakePHP - How to do reverse routing with slug?

前端 未结 4 1088
囚心锁ツ
囚心锁ツ 2021-01-13 12:12

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

If I have a product that is

4条回答
  •  醉梦人生
    2021-01-13 13:08

    For routing:

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

    Your links should look like this:

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

    You have to add additional (key => value) to your array for each :param in your routing. Then magic will work

提交回复
热议问题