多个视图一个控制器-功能


Cakemultiple views one controller-Function

我正在使用CakePHP几个星期。我已经用控制台创建了CRUD功能和分页。对于布局,我使用了bootstrap插件。该网站旨在促进竞争过程。现在我有一个问题:在我的示例中,有不同的用户角色。对于一个角色,我们需要一个在16个视图中使用的函数,只是每次另一个表内容。但是我不能把同一个函数写16遍?谢谢你的建议

将该函数添加到AppController中,由每个控制器扩展。

AppController示例

App::uses('Controller', 'Controller');
class AppController extends Controller {
    public $components = array('DebugKit.Toolbar');
    function usedInDifferentControllers($param1, $param2){
        //code
    }
}

PagesController示例

App::uses('AppController', 'Controller');
class PagesController extends AppController {
    function action_name(){
        //call the function from  AppController
        $this->usedInDifferentControllers($param1, $param2); 
    }
    //optionally, you can override the method if required for each controller
    function usedInDifferentControllers(){
        parent::usedInDifferentControllers();
    }
}

也许我说的是假的:在16个视图中使用的动作,只需要每次另一个表记录。我的意思是,它是同一个表,只是来自这个表的另一条记录:/所以我只有一张表!函数(动作)总是相同的。

//例子表(firstname,姓,number1,科学number3,等等…)

//在控制器中dosomething () {}

//在视图中dosomething。CTP(名、姓、号)dosomething1。CTP(名、姓、号)dosomething2。CTP(名,姓,号)

在最后,我将有一个函数,它将计算这些数字在新视图中:)

我像这样搜索:

 public function index() {
    // 0 CakePHP fetches Group data and its domain
        $groupId=$this->Auth->user('User.role_id');
   if($groupId==1) $this->view='viewA';
   else if($groupId==4) $this->view='ViewB';
    $participants = $this->Participant->recursive = 0;
        $this->set('participants',$this->Paginator->paginate());
}

但是它不是这样工作的:(