cakephp 一种视图可显示多种结果

发布于 2025-01-01 02:38:52 字数 1648 浏览 2 评论 0原文

我有一些控制器逻辑:

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




}

这会重复 13 次,有 13 个不同的视图页面,标记为“海鲜、海滨、牛排馆”等。视图完全相同,一切都一样,除了控制器必须通过特定的查找餐厅类型。有人可以向我解释一下如何制作一个显示在 www.site.com/restaurants/seafood 上的视图文件吗?

说实话,我所有的结果页面都与此有所不同。我告诉 cake 对某些模型的数据进行分页(通常包含),然后将其粘贴到与所有其他页面几乎相同(有一个图标或两个差异)的视图中。我正在建立一个网站,它是“在 x 海滩小镇要做和看的事情”之一,所以我有餐厅、住宿地点、购物、夜总会、高尔夫球场等(到处都是)。

我的老板给了我一个庞大的网站来构建,但我真的不太了解任何编程逻辑。我想在这里坚持 DRY 概念,这样我才能真正学习这些东西。

更新

好吧,我确保我的路线在文件中包含此内容:

Router::connect('/restaurants/:action', array('controller'=>'restaurants'));

我保留了我的seafood.ctp文件,然后取出我的seafood()函数并将其插入:

 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);
}   

当我访问 www 页面时.site.com/restaurants/seafood cake 告诉我,我的控制器缺少海鲜()方法。我错过了什么?

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 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 www.site.com/restaurants/seafood cake told me that my controller was missing the method seafood(). What did I miss?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野稚 2025-01-08 02:38:52

试试这个:

Router::connect('/restaurants/:restaurantType', array('controller'=>'restaurants', 'action' => 'restaurants'));

它没有找到它的原因是因为您仍在使用标准 :action 参数。您只需要更改它,以便操作保持不变,这样它就会作为参数传递到操作中。

Try this:

Router::connect('/restaurants/:restaurantType', array('controller'=>'restaurants', 'action' => 'restaurants'));

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.

相守太难 2025-01-08 02:38:52

为什么不给控制器操作一个通用名称,例如

public function restaurant($restaurantType) {

// Pass the parameter in via the URL for the restaurant type and use that parameter 
// as your find condition as well as the value for the title of the page

}

,然后用于打开页面的链接的 URL 看起来像
http://www.whatever.com/restaurant/seafood

Why don't you just give the controller action a generic name like

public function restaurant($restaurantType) {

// Pass the parameter in via the URL for the restaurant type and use that parameter 
// as your find condition as well as the value for the title of the page

}

And then your URL for your link to bring up the page would look like
http://www.whatever.com/restaurant/seafood

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文