0

totally new to cakephp and really struggling to understand the docs.

Idea I'm working on is for a rental property search. I have controller rentalsController

In here I have index which I fetch all

I also have a sidebar which lists regions

Now, what I want to happen is for a user to view index and then go to sidebar and select a region they'd wish to rent from. The URL should be .com/rentals/region-name/

This will then call (could be wrong here) rentalsController > byRegion($region){ fetch.... )

How do I a) set-up routes to manage this and b) the function to gather that passed region.

Sorry if this basic but I've searched and now about to blow my mind - as you can imagine - we've all been here at one point learning a new way of doing things.

Thank you for all your feedback - Mark

EDIT

Finally got there:

Router::connect(
    '/rentals-in-:region.html', 
    array( 'controller' => 'rentals', 'action' => 'byRegion' ),
    array( 'region' => '[a-zA-Z0-9\-]+', 'pass' => array('region'),
));
4

1 回答 1

1

a)如果您是蛋糕新手,我不建议您在路线文件中玩耍。如果您遵循 cake 的约定,您可以通过调用 /rentals/by_region/{region_name} 访问您的 RentalsController::byRegion ($ region) 操作。

不要忘记创建您的视图文件 app/View/Rentals/by_region.ctp

b) 假设您的 rents 表有一个 region 字段:

public function byRegion ($region){
    $this->set ('rentals', $this->Rental->find ('all', array (
        'conditions' => array (
            'Rental.region LIKE' => $ region
        )
    )));

编辑

比您正在寻找的:

Router::connect(
   '/rentals/:region',
   array('action' => 'byRegion'),
   array('region' => '{your regex matching region}')
);

将此添加到您的路线文件中。您现在应该能够像您提到的那样调用 url。应该可以,但我没有测试过。

于 2015-02-15T21:17:10.197 回答