简体   繁体   English

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

[英]Cakephp - multiple views one controller-Function

I'm working with CakePHP for a few weeks. 我和CakePHP一起工作了几个星期。 I have created with the Console the CRUD functionality and Pagination. 我已经使用控制台创建了CRUD功能和分页。 For the layout I used the plugin from bootstrap. 对于布局,我使用了bootstrap中的插件。 The website is designed to facilitate a competitive process. 该网站旨在促进竞争过程。 Now I have a question: In my example, there are different user roles. 现在我有一个问题:在我的示例中,有不同的用户角色。 For a role, we need a function which is used in 16 Views, just every time another table content. 对于角色,我们需要一个在每次其他表内容出现时都在16个视图中使用的函数。 But I can not write the same function 16 times? 但是我不能写相同的函数16次吗? Thank you for your advice :) 感谢您的意见 :)

Add that function in AppController, which is extends by each Controllers. 在AppController中添加该功能,该功能由每个Controller扩展。

Example of AppController AppController的示例

App::uses('Controller', 'Controller');
class AppController extends Controller {
    public $components = array('DebugKit.Toolbar');

    function usedInDifferentControllers($param1, $param2){
        //code
    }
}

Example of PagesController 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();
    }
}

Perhaps I said it false: the action which is used in the 16 Views, need just every time another table record. 也许我说错了:这16个视图中使用的操作每次都需要另一个表记录。 I mean its the same table but another record from this table :/ So I have only one table! 我的意思是它是同一张桌子,但是这张桌子上有另外一条记录://所以我只有一张桌子! the function (action) is always the same. 功能(动作)始终相同。

//Example table ( firstname, lastname, number1, number2, number3, and so on...) //示例表(名字,姓氏,数字1,数字2,数字3,等等...)

//in the controller dosomething(){ } //在控制器中执行dosomething(){}

//in the view dosomething.ctp (firstname, lastname, number1) dosomething1.ctp (firstname, lastname, number2) dosomething2.ctp (firstname, lastname, number3) //在视图中dosomething.ctp(名字,姓氏,数字1)dosomething1.ctp(名字,姓氏,数字2)dosomething2.ctp(名字,姓氏,数字3)

At the End I will have a function, which will calculate with this numbers in an new view :) 最后,我将有一个函数,它将在新视图中使用此数字进行计算:)

I search something like that: 我搜索类似的东西:

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

But it doesn't works like that:( 但这不是这样的:(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM