【问题标题】:Cakephp - multiple views one controller-FunctionCakephp - 多个视图一个控制器-功能
【发布时间】:2014-06-01 13:38:31
【问题描述】:

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

【问题讨论】:

    标签: php cakephp view controller


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      也许我说错了:在16个视图中使用的动作,每次都需要另一个表记录。我的意思是它的同一张表,但该表中的另一条记录:/ 所以我只有一张桌子!功能(动作)总是一样的。

      //示例 桌子 ( 名, 姓, 1号, 2号, 编号 3, 等等……)

      //在控制器中 做某事(){}

      //在视图中 dosomething.ctp(名字,姓氏,数字1) dosomething1.ctp(名字,姓氏,数字2) dosomething2.ctp (firstname, lastname, number3)

      最后我会有一个函数,它会用这个数字进行计算 在一个新的观点:)

      我搜索类似的东西:

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

      但它不是那样工作的:(

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-02
        相关资源
        最近更新 更多