Gravesy
Gravesy

Reputation: 45

Cakephp 2 Routing / SEO friendly urls

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'),
));

Upvotes: 0

Views: 463

Answers (1)

Ben
Ben

Reputation: 683

a) If you are new to cake I wouldn't recommend to play around in your routes file. If you follow cake's conventions you could access your RentalsController::byRegion ($ region) action by calling /rentals/by_region/{region_name}.

Don't forget to create your view file app/View/Rentals/by_region.ctp

b) Assuming your rentals table has a region field:

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

edit:

Than you are looking for:

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

Add this to your routes file. You now should be able to call the url like you mentioned. Should work but I haven't tested it.

Upvotes: 1

Related Questions