Kohana控制器实例化视图问题

发布于 2024-10-16 02:54:16 字数 4219 浏览 2 评论 0原文

我正在编写一个 kohana 控制器,并且不断收到一个奇怪的错误,我猜这是关于视图的错误实例化,但我不知道它在哪里。我的控制器:

<?php if (!defined('SYSPATH')) exit('No direct script access allowed');
/**
  * Contact_Controller - Used for validating and sending emails from form.
  *
  */
  class Controller_Contact extends Controller
  {

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

    public function action_index()
    {
        //Setup view without rendering
        //Setup base view
        $view = new View('template');

        //Setup header
    //  $view->header = new View('header');

        //Setup header title
        //$view->header->title = 'Kohana Contact Form by Drew Douglass';

        //Setup content view
        $view->content = new View('contact');

        //Setup footer view
        //$view->footer = new View('footer');

        //Setup default form values so we can repopulate the form
        $form = array
        (
            'contact_name' => '',
            'contact_email' => '',
            'contact_subject' => '',
            'contact_message' => ''
        );

        //copy the form fields into the errors for later...
        $errors = $form; 

        //Check to see if the form was submitted
        if(isset($_POST['contact_submit']))
        {
            //Add the rules we need to validate our form
            $post = new Validation($_POST);

            //filter the whitespace from beginning and ends of fields
            $post->pre_filter('trim');

            //Add rules for contact_name
            $post->add_rules('contact_name', 'required', 'standard_text', 'length[2,20]');

            //Add rules for contact_email
            $post->add_rules('contact_email', 'required', 'email', 'email_domain');

            //add rules for subject
            $post->add_rules('contact_subject', 'required', 'length[5,30]', 'standard_text');

            //Add rules for message
            $post->add_rules('contact_message', 'required', 'standard_text');

            //If there were no errors...
            if($post->validate())
            {
                //Load the config file with our email address defaults
                //This make our app more portable
                $email_config = Kohana::config_load('email');

                //Send it and render the view with message.
                $to = $email_config['default_email'];
                $from = $this->input->post('contact_email');
                $subject = $this->input->post('contact_subject');
                $message = $this->input->post('contact_message');

                //Add some info to the end of the message for reference
                $message .= "\n\n This message was sent to you from".$this->input->post('contact_name')." and the email given is ".$from;

                if(email::send($to, $from, $subject, $message, TRUE))
                {
                    $view->content->set('form_values', $form);
                    $view->content->set('message', 'Your email was sent!');
                    $view->render(TRUE);
                }
                else
                {
                    die('We had a technical error, please try again.');
                }
            }

            //else we got errors...
            else
            {
                //repopulate form values
                $form = arr::overwrite($errors, $post->as_array());

                //setup the errors
                $errors = arr::overwrite($errors, $post->errors('form_errors'));

                //pass the form values and errors to the view
                $view->content->set('errors', $errors);
                $view->content->set('form_values', $form);

                //Render the view with errors and values
                $view->render(TRUE);
            }

        }
        //form was not submitted, just show it.
        else
        {
            $view->content->set('form_values', $form);
            $view->render(TRUE);
        }

    }

  }

以及持续存在的错误: Kohana_View_Exception [ 0 ]:找不到请求的视图 1

为什么会这样?

谢谢

i am writing a kohana controller, and i am constantly getting a weired error, i guess it is about a wrong instantiation of the view, but i can't figure out where is it. My controller:

<?php if (!defined('SYSPATH')) exit('No direct script access allowed');
/**
  * Contact_Controller - Used for validating and sending emails from form.
  *
  */
  class Controller_Contact extends Controller
  {

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

    public function action_index()
    {
        //Setup view without rendering
        //Setup base view
        $view = new View('template');

        //Setup header
    //  $view->header = new View('header');

        //Setup header title
        //$view->header->title = 'Kohana Contact Form by Drew Douglass';

        //Setup content view
        $view->content = new View('contact');

        //Setup footer view
        //$view->footer = new View('footer');

        //Setup default form values so we can repopulate the form
        $form = array
        (
            'contact_name' => '',
            'contact_email' => '',
            'contact_subject' => '',
            'contact_message' => ''
        );

        //copy the form fields into the errors for later...
        $errors = $form; 

        //Check to see if the form was submitted
        if(isset($_POST['contact_submit']))
        {
            //Add the rules we need to validate our form
            $post = new Validation($_POST);

            //filter the whitespace from beginning and ends of fields
            $post->pre_filter('trim');

            //Add rules for contact_name
            $post->add_rules('contact_name', 'required', 'standard_text', 'length[2,20]');

            //Add rules for contact_email
            $post->add_rules('contact_email', 'required', 'email', 'email_domain');

            //add rules for subject
            $post->add_rules('contact_subject', 'required', 'length[5,30]', 'standard_text');

            //Add rules for message
            $post->add_rules('contact_message', 'required', 'standard_text');

            //If there were no errors...
            if($post->validate())
            {
                //Load the config file with our email address defaults
                //This make our app more portable
                $email_config = Kohana::config_load('email');

                //Send it and render the view with message.
                $to = $email_config['default_email'];
                $from = $this->input->post('contact_email');
                $subject = $this->input->post('contact_subject');
                $message = $this->input->post('contact_message');

                //Add some info to the end of the message for reference
                $message .= "\n\n This message was sent to you from".$this->input->post('contact_name')." and the email given is ".$from;

                if(email::send($to, $from, $subject, $message, TRUE))
                {
                    $view->content->set('form_values', $form);
                    $view->content->set('message', 'Your email was sent!');
                    $view->render(TRUE);
                }
                else
                {
                    die('We had a technical error, please try again.');
                }
            }

            //else we got errors...
            else
            {
                //repopulate form values
                $form = arr::overwrite($errors, $post->as_array());

                //setup the errors
                $errors = arr::overwrite($errors, $post->errors('form_errors'));

                //pass the form values and errors to the view
                $view->content->set('errors', $errors);
                $view->content->set('form_values', $form);

                //Render the view with errors and values
                $view->render(TRUE);
            }

        }
        //form was not submitted, just show it.
        else
        {
            $view->content->set('form_values', $form);
            $view->render(TRUE);
        }

    }

  }

and the persisting error:
Kohana_View_Exception [ 0 ]: The requested view 1 could not be found

Why can this be??

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

裸钻 2024-10-23 02:54:16

问题出在 $view->render(TRUE); 这是 Kohana2 语法,但在 Kohana3 中,要渲染的参数是视图名称,导致错误 (true =>render) 1)。

The problem is in $view->render(TRUE); This is Kohana2 syntax, but in Kohana3, the parameter to render is the view name, causing the error (true => 1).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文