简单的PHP数组codeigniter控制器

时间:2016-02-26 10:23:57

标签: php arrays codeigniter controller

我想稍微缩短我的CodeIgniter控制器。 现在它看起来像这样:

case 'klant1':
                $data['title'] = $page;
                $this->load->view('templates/header', $data);
                $this->load->view('pages/klant1', $data);
                $this->load->view('templates/footer', $data);
                break;
case 'klant2':
                $data['title'] = $page;
                $this->load->view('templates/header', $data);
                $this->load->view('pages/klant2', $data);
                $this->load->view('templates/footer', $data);
                break;
case 'klant3':
                $data['title'] = $page;
                $this->load->view('templates/header', $data);
                $this->load->view('pages/klant3', $data);
                $this->load->view('templates/footer', $data);
                break;

我想可能有办法使用数组或使klant1 + 2 + 3成为某种东西?

有人可以帮我解决问题吗?我不认为这很难,但我对PHP不好......

4 个答案:

答案 0 :(得分:0)

$klan = your_case;    

$data['title'] = $page;    
$this->load->view('templates/header', $data);    
$this->load->view('pages/'.$klan, $data);    
$this->load->view('templates/footer', $data)

答案 1 :(得分:0)

替换

switch($variable)
{
...your code 

$data['title'] = $page;    
$this->load->view('templates/header', $data);    
$this->load->view('pages/'.$variable, $data);    
$this->load->view('templates/footer', $data);

答案 2 :(得分:0)

class SomeClassCaller
{
    public void DoSomething()
    {
        var logger = DependencyContainer.Resolve<ILogger>();
        var someOtherClass = DependencyContainer.Resolve<...>();

        var someClass = new SomeClass(logger, someOtherClass);
        someClass.DoSomething();
    }
}

答案 3 :(得分:0)

这样做了:

function view($page = 'home', $klant = null) {
case 'klant':
                $data['title'] = $page;
                $this->load->view('templates/header', $data);
                $loadpage = 'pages/klant'.$klant;
                $this->load->view($loadpage, $data);
                $this->load->view('templates/footer', $data);
                break;
}