【问题标题】:CodeIgniter - Code repetition and passing variables to functionsCodeIgniter - 代码重复并将变量传递给函数
【发布时间】:2011-04-11 18:36:46
【问题描述】:

我在控制器中有以下代码:

<?php

class Student extends CI_Controller
{

function index()
{
    $data = $this->init->set();
    
    $this->parser->parse('include/header', $data);
    $this->parser->parse('student/student_index', $data);
    $this->parser->parse('include/footer', $data);
}

function planner()
{
    $data = $this->init->set();
    
    $this->parser->parse('include/header', $data);
    $this->parser->parse('student/student_cal', $data);
    $this->parser->parse('include/footer', $data);      
}

}
?>

如您所见,这里有很多重复。基本上都是。我已经将变量放入模型中,因此我只需每次调用​​模型函数,而不是将整个 $data 数组放在每个函数的开头。无论如何,我尝试通过执行以下操作来减少此处的重复:

<?php

class Student extends CI_Controller
{

function index()
{
    $data = $this->init->set();
    
    $this->parser->parse('include/header', $data);
    
    switch($this->uri->segment(2))
    {
        case '': $this->home($data); break;
        case 'planner': $this->planner($data); break;
    }
    $this->parser->parse('include/footer', $data);
}

function home($data)
{
    $this->parser->parse('student/student_index', $data);
}


function planner($data)
{
    $this->parser->parse('student/student_cal', $data);
}

}
?>

不知何故,这对我的主页很有效。它解析变量,没有任何问题。但是,在“计划者”页面上,出现错误:

消息:Student::planner() 缺少参数 1

消息:未定义变量:数据

消息:为 foreach() 提供的参数无效

我很确定我得到了这些错误,因为该函数不知何故没有收到$data 数组。我还在 CI 文档中读到 URL 中的第三段作为参数传递,在这种情况下,第三段不存在,因此没有任何传递。但是,CI 文档没有告诉我如何将 $data 数组从 index() 函数传递给 planner() 函数。我也想知道为什么 home 功能可以正常工作,没有错误。

【问题讨论】:

    标签: php codeigniter codeigniter-2


    【解决方案1】:

    现在,如果重构会让代码变得很难看,我看不出重构的原因。我不完全确定 parse 函数的作用,所以我更改它的方式是将参数作为字符串实际传递,但最好将内容加载到缓冲区中并以这种方式传递。但是这里有一些更干净且希望可读的重复删除...希望它可以工作:)。

    
    
    class Student extends CI_Controller
    {
    
      private function load_student_page($content){
          $data = $this->init->set();
    
          $this->parser->parse('include/header', $data);
          $this->parser->parse($content, $data);
          $this->parser->parse('include/footer', $data);
    
      }
    
      function index()
      {
        $this->load_student_page('student/student_index');
      }
    
      function planner()
      {
        $this->load_student_page('student/student_cal');
      }
    
    }
    

    【讨论】:

      【解决方案2】:

      正如你所说,CodeIgniter 正在尝试将第三个段作为参数传入,但它不存在。

      您可能需要使用“_remap”函数。

      class Student extends CI_Controller {
      
          public function _remap($method, $parameters)
          {
               $data = $this->init->set();
               $this->parser->parse('include/header', $data);
      
               switch($this->uri->segment(2))
               {
                   case '': $this->home($data); break;
                   case 'planner': $this->planner($data); break;
               }
      
               $this->parser->parse('include/footer', $data);
          }
      

      }

      【讨论】:

      • 嗯,我试过了,但我无法让它工作。我想我以后会搞砸的;在这里尝试其他“解决方案”有点浪费时间。我之前正在研究 _remap 函数,但它并不能真正做到我想要它做的事情。也许我用错了! :) 我会再看一遍,也许会查一些模板.. 我想我真的不明白这个框架:(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-08
      • 2013-05-01
      • 1970-01-01
      相关资源
      最近更新 更多