将多个CodeIgniter控制器合并到同一视图中

时间:2012-08-19 23:44:58

标签: php codeigniter

所以我有一个显示站点布局的控制器,然后我有另一个管理电子邮件的控制器。我的问题是如何将它们组合到同一页面上(相同的视图)我只是将我的电子邮件控制器的功能添加到我的站点控制器?那么站点控制器有send()方法,然后将所有内容从我的“简报”视图移到我的“主页”视图中?

站点控制器

<?php

      class Site extends CI_Controller 
   {

    function index()
      {
        $this->load->model('site_model');
        $data['records']= $this->site_model->getAll();
        $this->load->view('home',$data);
      }

   }

 ?>

电子邮件控制器

class Email extends CI_Controller{


    function index()
    {

        $this->load->view('newsletter');
    }

    function send()
    {
        $this->load->library('form_validation');

        //field name, error message, validaion rules
        $this->form_validation->set_rules('name', 'Name', 'trim|required');
        $this->form_validation->set_rules('name', 'Email Address', 'required');


        if($this->form_validation->run()==FALSE)
        {
            $this->load->view('newsletter');

        }

        else
        {
        $name = $this->input->post('name');
        $email = $this->input->post('email');

        $this->load->library('email');
        $this->email->set_newline("\r\n");


        $this->email->from('', '');
        $this->email->to($email);
        $this->email->subject('This is an email test');
        $this->email->message('Sucess');

        $path = $this->config->item('server_root');
        $file = $path  .'CodeTest/attachments/newsletter1.txt';

        $this->email->attach($file);

        if($this->email->send())
        {
            echo ' Your email has been sent.';
        }

        else {

            show_error($this->email->print_debugger());

             }

        }

    }


}

1 个答案:

答案 0 :(得分:0)

您是否从站点控制器中的方法中删除了此代码块?
此代码会自动转发到简报页面。您必须从站点控制器中删除它

if($this->form_validation->run()==FALSE){
  $this->load->view('newsletter');
}