从Controller问题

时间:2018-06-12 04:59:18

标签: php codeigniter

我是CodeIgniter框架的新手,需要别人的帮助。 我有一个问题是在提交后将用户重定向到“联系我们”表单。

我的默认控制器定义如下。

<?php

class Pages extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function view($page = 'home') {
        if (!file_exists(APPPATH . 'views/pages/' . $page . '.php')) {
            show_404();
        }

        $data['title'] = ucfirst($page);

        $this->load->view('templates/header');
        $this->load->view('pages/' . $page, $data);
        $this->load->view('templates/footer');
    }

} 

现在我的视图页面位于名为pages的子目录中,例如/application/views/pages/contact-us.php。因此,当我像http://www.example.com/contact-us那样访问此页面时,它可以正常工作并按预期加载联系我们表单。

查看我的.htaccess文件,该文件从URL中删除了index.php。

RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\)?example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R,L]
RewriteCond $1 !^(index\.php|assets|images|js|css|uploads|favicon.png|resources|robots\.txt)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

还要查看我的routes.php文件。

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

$route['sendemail'] = 'sendemail/index';

$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';

$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE; 

现在用户提交联系我们表单后,它会转到名为SendEmail.php的控制器,然后我希望用户返回联系我们表单,其中包含成功或失败的消息。我收到一封电子邮件到我的Gmail帐户,但是当它加载VIEW时它不起作用。

查看SendEmail.php的控制器代码

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class SendEmail extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        // Check form submit or not
        //var_dump($this->input->post());

        if ($this->input->post('submit') != NULL) {

            // POST data
            $postData = $this->input->post();

            //SMTP related configuration is defined in /config/email.php
            //it loads automatically by CodIgniter
            
            $this->load->library('email');
            $this->email->set_newline("\r\n");

            $this->email->from($postData['email'], $postData['name']);
            $this->email->to(ADMIN_CURRENT_EMAIL);

            $this->email->subject(CONTACT_US_FORM_SUBJECT);
            $this->email->message('Testing the email class.');

            $data['is_error'] = false;

            if ($this->email->send()) {
                $data['message'] = 'The email has been sent successfully.';
            } else {
                $data['is_error'] = true;
                $data['message'] = 'Sorry, the email was not sent successfully. Try again later or send a direct email to ' . ADMIN_CURRENT_EMAIL;
                //echo $this->email->print_debugger();
            }
            
            $this->load->view('contact-us', $data);           
            
        } else {
            echo "Invalid request.";
            die();
        }
    }

}

?>

我收到以下错误。正如我上面所说,我设法收到电子邮件到我的Gmail帐户,但加载视图不起作用。

遇到错误 无法加载请求的文件:contact-us.php

你能帮帮我吗?

谢谢 - Hitesh

1 个答案:

答案 0 :(得分:0)

要重定向到最后一页,可以使用此代码。

redirect($_SERVER['HTTP_REFERER']);