I have some controller logic:
public function seafood() {
$this->set('title', 'Seafood restaurants in and near Gulf Shores, AL');
$this->paginate['Restaurant']=array(
'limit'=>9,
'order' => 'RAND()',
'conditions'=>array(
'Restaurant.active'=>1,
'Restaurant.seafood'=>'Seafood'
)
);
$data = $this->paginate('Restaurant');
$this->set('seafood', $data);
}
This gets repeated like 13 times, with 13 different view pages labeled “seafood, waterfront, steakhouse” etc. The view is exactly the same, everthing is the same really, except the controller has to find by a specific type of restaurant. Can someone please explain to me how I can just make one view file that would show up at say http://www.site.com/restaurants/seafood?
Truth be told, all of my results pages are some variation on this. I tell cake to paginate (and usually contain) some model’s data, then stick it into a view that is almost identical (with an icon or two difference) to all the other pages. I am building a site that is one of those “things to do and see in x beach town”, so I have restaurants, places to stay, shopping, nightclubs, golf courses, etc (it is all over the place).
My boss has given me this gargantuan website to build and I really don’t know any programming logic very well. I’d like to stick to the DRY concept here so that I can really learn this stuff.
UPDATE
Ok, I made sure my routes had this in the file:
Router::connect('/restaurants/:action', array('controller'=>'restaurants'));
I kept my seafood.ctp file, then took out my seafood() function and stuck this in instead:
public function restaurants($restaurantType) {
$this->set('title', $restaurantType.' restaurants in and near Gulf Shores, AL');
$this->paginate['Restaurant']=array(
'limit'=>9,
'order' => 'RAND()',
'conditions'=>array(
'Restaurant.active'=>1,
'Restaurant.seafood'=>$restaurantType
)
);
$data = $this->paginate('Restaurant');
$this->set($restaurantType, $data);
}
When I accessed the page at http://www.site.com/restaurants/seafood cake told me that my controller was missing the method seafood(). What did I miss?
Try this:
The reason it’s not finding it, is because you’re still using the standard :action parameter. You just need to change it so the action remains the same, that way it gets passed into the action as parameter.