Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Controller cakephp一个视图用于多种类型的结果_Controller_Cakephp 2.0_Cakephp Model - Fatal编程技术网

Controller cakephp一个视图用于多种类型的结果

Controller cakephp一个视图用于多种类型的结果,controller,cakephp-2.0,cakephp-model,Controller,Cakephp 2.0,Cakephp Model,我有一些控制器逻辑: public function seafood() { $this->set('title', 'Seafood restaurants in and near Gulf Shores, AL'); $this->paginate['Restaurant']=array( 'limit'=>9, 'order' => 'RAND()', 'conditions'=>array(

我有一些控制器逻辑:

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/seafiod上

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

我的老板给了我这个庞大的网站,我真的不知道任何编程逻辑非常好。我想在这里坚持干燥的概念,这样我才能真正学到这些东西

更新

好的,我确保我的路线在文件中有以下内容:

Router::connect('/restaurants/:action', array('controller'=>'restaurants'));
我保留了seafiod.ctp文件,然后取出seafiod()函数并将其插入:

 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/seafiod cake的页面时,我告诉我我的控制器缺少seafiod()方法。我错过了什么?

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

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会显示页面 试试这个:

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

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

我认为您尝试更改路由时把事情搞砸了<代码>路由器::连接('/restaurants/:action',数组('controller'=>'restaurants')你告诉它把餐馆后面的东西作为一个动作,而实际上,
餐馆
是动作,后面的东西是参数,所以很自然,现在它告诉你它找不到函数名
海鲜()
我删除了router::connect规则,它仍然给我海鲜()控制器错误中缺少。错误指向的控制器的名称是什么?实际控制器的名称是什么?我认为您只是在如何定义URL方面遇到了问题。它应该类似于www.mysite.com/controllername/actionname/parameter。这就是cake在解析URL时所寻找的。它说问题出在RestaurantController中。而我的实际控制器是restaurantsOk,在我的控制器中做了很多手脚之后,使用这张罚单。非常感谢。