如何将模型文件调用到控制器并在opencart中查看?

时间:2013-03-26 12:52:22

标签: php opencart

我是Open Cart框架工作的新手。我正在创建带有文件上传和直接结帐按钮的自定义页面(就像购物车页面一样。)在这个页面中,我已经根据联系表单创建了(tpl files)视图和控制器。 我的控制器在这里:

<?php 
class ControllerInformationRequest extends Controller {
    private $error = array(); 

    public function index() {
        $this->language->load('information/request');

        $this->document->setTitle($this->language->get('heading_title'));  
        $this->load->model('account/request');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {

            $this->redirect($this->url->link('information/request/success'));
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_home'),
            'href'      => $this->url->link('common/home'),         
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('heading_title'),
            'href'      => $this->url->link('information/request'),
            'separator' => $this->language->get('text_separator')
        );  

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_location'] = $this->language->get('text_location');
        $this->data['text_contact'] = $this->language->get('text_contact');
        $this->data['text_address'] = $this->language->get('text_address');
        $this->data['text_telephone'] = $this->language->get('text_telephone');
        $this->data['text_fax'] = $this->language->get('text_fax');
        $this->data['entry_name'] = $this->language->get('entry_name');
        $this->data['entry_email'] = $this->language->get('entry_email');
        $this->data['entry_enquiry'] = $this->language->get('entry_enquiry');
        $this->data['entry_captcha'] = $this->language->get('entry_captcha');
        $this->data['entry_phone']  = $this->language->get('entry_phone');

        if (isset($this->error['name'])) {
            $this->data['error_name'] = $this->error['name'];
        } else {
            $this->data['error_name'] = '';
        }
        if (isset($this->error['telephone'])) { 
            $this->data['error_telephone'] = $this->error['telephone'];   
        } else {      
             $this->data['error_telephone'] = '';       
        } 
        if (isset($this->error['email'])) {
            $this->data['error_email'] = $this->error['email'];
        } else {
            $this->data['error_email'] = '';
        }
        if (isset($this->error['enquiry'])) {
            $this->data['error_enquiry'] = $this->error['enquiry'];
        } else {
            $this->data['error_enquiry'] = '';
        }       

        if (isset($this->error['captcha'])) {
            $this->data['error_captcha'] = $this->error['captcha'];
        } else {
            $this->data['error_captcha'] = '';
        }   

        $this->data['button_continue'] = $this->language->get('button_continue');

        $this->data['action'] = $this->url->link('information/request');
        $this->data['store'] = $this->config->get('config_name');
        $this->data['address'] = nl2br($this->config->get('config_address'));
        $this->data['telephone'] = $this->config->get('config_telephone');
        $this->data['fax'] = $this->config->get('config_fax');

        if (isset($this->request->post['name'])) {
            $this->data['name'] = $this->request->post['name'];
        } else {
            $this->data['name'] = $this->customer->getFirstName();
        }

        if (isset($this->request->post['email'])) {
            $this->data['email'] = $this->request->post['email'];
        } else {
            $this->data['email'] = $this->customer->getEmail();
        }

        if (isset($this->request->post['telephone'])) {
            $this->data['telephone'] = $this->request->post['telephone'];
        } else {
            //$this->data['phone'] = $this->customer->getPhone();
            $this->data['telephone'] = $this->customer->getTelephone();
        }
        if (isset($this->request->post['enquiry'])) {
            $this->data['enquiry'] = $this->request->post['enquiry'];
        } else {
            $this->data['enquiry'] = '';
        }

        if (isset($this->request->post['captcha'])) {
            $this->data['captcha'] = $this->request->post['captcha'];
        } else {
            $this->data['captcha'] = '';
        }       

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/information/request.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/information/request.tpl';
        } else {
            $this->template = 'default/template/information/request.tpl';
        }
        $this->load->model('account/request');
        $this->children = array(
            'common/column_left',
            'common/column_right',
            'common/content_top',
            'common/content_bottom',
            'common/footer',
            'common/header'
        );

        $this->response->setOutput($this->render());        
    }

    public function success() {
        $this->language->load('information/request');

        $this->document->setTitle($this->language->get('heading_title')); 

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_home'),
            'href'      => $this->url->link('common/home'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('heading_title'),
            'href'      => $this->url->link('information/request'),
            'separator' => $this->language->get('text_separator')
        );  

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_message'] = $this->language->get('text_message');

        $this->data['button_continue'] = $this->language->get('button_continue');

        $this->data['continue'] = $this->url->link('common/home');

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/success.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/common/success.tpl';
        } else {
            $this->template = 'default/template/common/success.tpl';
        }

        $this->children = array(
            'common/column_left',
            'common/column_right',
            'common/content_top',
            'common/content_bottom',
            'common/footer',
            'common/header'
        );

        $this->response->setOutput($this->render()); 
    }

    private function validate() {
        if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 32)) {
            $this->error['name'] = $this->language->get('error_name');
        }

        if (!preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['email'])) {
            $this->error['email'] = $this->language->get('error_email');
        }

        if ((utf8_strlen($this->request->post['enquiry']) < 10) || (utf8_strlen($this->request->post['enquiry']) > 3000)) {
            $this->error['enquiry'] = $this->language->get('error_enquiry');
        }

        if (empty($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
            $this->error['captcha'] = $this->language->get('error_captcha');
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }     
    }

    public function captcha() {
        $this->load->library('captcha');

        $captcha = new Captcha();

        $this->session->data['captcha'] = $captcha->getCode();

        $captcha->showImage();
    }   
}
?>

在视图.tpl文件中包含姓名,电子邮件地址,电话,消息和验证码图片。我只想将该数据存储到数据库中,我已经在特定位置创建了模型文件, 如何调用模型文件($this->load->model('account/request');)...它也没有t working and its can t存储数据库。怎么可能?请为我提供解决方案....

1 个答案:

答案 0 :(得分:1)

从控制器加载模型使用此语法;

例如加载opencart文件夹/ catalog / model / catalog / category

$this->load->model('catalog/category');

然后您可以愉快地从该模型中调用方法,如下所示;

$categories = $this->model_catalog_category->getCategories(0);