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('for开发者_运维知识库m_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
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
).
精彩评论