Controller not acting correctly
I'm new to code igniter but I've been watching many youtube videos and I'm starting to get the hang of the basics of it however after I do a test run on my registration form it goes to a white page with The requested URL /kowmanager/user/register was not found on this server. I'm not sure why. Any ideas?
Controller:
function User()
{
parent :: __construct();
$this->view_data['base_url'] = base_url();
$this->load->model('User_model');
}
function index()
{
$this->register();
}
function register()
{
$this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_numeric|min_length[6]|xss_clean|strtolower|callback_usernameNotExists');
$this->form_validation->set_rules('password', 'Password', 'trim|required|alpha_numeric|min_length[6]|xss_clean');
$this->form_validation->set_rules('passwordConfirm', 'Confirm Password', 'trim|required|alpha_numeric|min_length[6]|xss_clean|matches[password]');
$this->form_validation->set_rules('email', 'Email', 'trim|required|min_length[6]|xss_clean|valid_email|callback_emailNotExists');
$this->form_validation->set_rules('firstName', 'First Name', 'trim|required|alpha_numeric|min_length[6]|xss_clean');
$this->form_validation->set_rules('lastName', 'Last Name', 'trim|required|alpha_numeric|min_length[6]|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('view_register', $this->view_data);
}
else
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$email = $this->input->post('email');
$firstName = $this->input->post('firstName');
$lastName = $this->input->post('lastName');
$registrationKey = substr(md5(mt_rand()), 0, 5);
$this->User_model->registerUser($username, $password, $email, $firstName, $lastName, $registrationKey);
}
}
function usernameNotExists($username)
{
$this->form_validation->set_message('usernameNotExists', ' That %s already exists inside the database!');
if($this->User_model->checkExistsUsername($username))
{
return false;
}
else
{
return true;
}
}
function emailNotExists($username)
{
$this->form_validation->set_message('emailNotExists', ' That %s already exists inside the database!');
if($this->User_model->checkExistsEmail($email))
{
return false;
}
else
{
return true;
}
}
}
?>
View Page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>KOW Manager Registration Form</title>
</head>
<body>
<?php
echo form_open($base_url . 'user/register');
$username = array ('name' => 'username', 'id' => 'username', 'value' => set_value('username'));
$password = array ('name' => 'password', 'id' => 'password', 'value' => '');
$passwordConfirm = array ('name' => 'passwordConfirm', 'id' => 'passwordConfirm', 'value' => '');
$email = array ('name' => 'email', 'id' => 'email', 'value' => set_value('email'));
$firstName = array ('name' => 'firstName', 'id' => 'firstName', 'value' => set_value('firstName'));
$lastName = array ('name' => 'lastName', 'id' => 'lastName', 'value' => set_value('lastName'));
?>
<?php echo form_fieldset('User Information') ?>
<dl>
<dt><label for="username">Username:</label></dt>
<dd><?php echo form_input($username); ?></dd>
</dl>
<dl>
<dt><label for="password">Password:</label></dt>
<dd><?php echo form_password($password); ?></dd>
</dl>
<dl>
<dt><label for="passwordConfirm">Confirm Password:</label></dt>
<dd><?php echo form_password($passwordConfirm); ?></dd>
</dl>
<dl>
<dt><label for="email">Email Address:</label></dt>
<dd><?php echo form_input($email); ?></dd>
</dl>
<dl>
<dt><label for="firstName">First Name:</label></dt>
<dd><?php echo form_input($firstName); ?></dd>
</dl>
<dl>
<dt><label for="lastName">Last Name:</label></dt>
<dd><?php echo form_input($lastName); ?></dd>
</dl>
<?php echo form_fieldset_close() ?>
<?php echo validation_errors() ?>
<dl class="sub开发者_运维技巧mit">
<?php echo form_submit(array('name' => 'register'), 'Register'); ?>
</dl>
<?php echo form_close(); ?>
</body>
</html>
Edit:
Here's my new code which is still doing the same thing.
<?php
class User extends CI_Controller {
function User()
{
parent :: __construct();
$this->view_data['base_url'] = base_url();
$this->load->model('User_model');
}
function index()
{
$this->register();
}
function register()
{
$this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_numeric|min_length[6]|xss_clean|strtolower|callback_usernameNotExists');
$this->form_validation->set_rules('password', 'Password', 'trim|required|alpha_numeric|min_length[6]|xss_clean');
$this->form_validation->set_rules('passwordConfirm', 'Confirm Password', 'trim|required|alpha_numeric|min_length[6]|xss_clean|matches[password]');
$this->form_validation->set_rules('email', 'Email', 'trim|required|min_length[6]|xss_clean|valid_email|callback_emailNotExists');
$this->form_validation->set_rules('firstName', 'First Name', 'trim|required|alpha_numeric|min_length[6]|xss_clean');
$this->form_validation->set_rules('lastName', 'Last Name', 'trim|required|alpha_numeric|min_length[6]|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('view_register', $this->view_data);
}
else
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$email = $this->input->post('email');
$firstName = $this->input->post('firstName');
$lastName = $this->input->post('lastName');
$registrationKey = substr(md5(mt_rand()), 0, 5);
$this->User_model->registerUser($username, $password, $email, $firstName, $lastName, $registrationKey);
$this->load->library('email');
$this->email->from('kowmanagement@kansasoutlawwrestling.com', 'KOW Management');
$this->email->to($email);
$this->email->subject('KOW Manager Account Registration');
$this->email->message('Hello '.$firstName.' '.$lastName.' Welcome to our website!<br /><br />You, or someone using your email address, has completed registration at '.myDomainName().'. You can complete registration by clicking the following link:<br /><br />' . anchor('http://www.'.myDomainName().'/manager/verify.php?userID='.$userID.'&verifyHash='.$verifyHash.'", http://www.'.myDomainName().'/manager/verify.php?userID='.$userID.'&verifyHash='.$verifyHash.''));
$this->email->send();
}
}
function registerConfirm()
{
$registrationKey = $this->uri->segment(3);
if ($registrationKey == '')
{
echo 'No registration key found in URL';
exist();
}
$registrationConfirmed = $this->User_model->confirmRegistration($registrationKey);
if ($registrationConfirmed)
{
echo 'You have successfully registered!';
}
else
{
echo 'You have failed to register!';
}
}
function usernameNotExists($username)
{
$this->form_validation->set_message('usernameNotExists', ' That %s already exists inside the database!');
if($this->User_model->checkExistsUsername($username))
{
return false;
}
else
{
return true;
}
}
function emailNotExists($username)
{
$this->form_validation->set_message('emailNotExists', ' That %s already exists inside the database!');
if($this->User_model->checkExistsEmail($email))
{
return false;
}
else
{
return true;
}
}
function myDomainName()
{
$my_domain = $_SERVER['HTTP_HOST'];
$my_domain = str_replace('www.', '', $my_domain);
return $my_domain;
}
}
?>
Any other ideas?
CodeIgniter routes by default are structured like this.
http://example.com/index.php/Controller/Function
If you don't have 'index.php' in your code it isn't going to be routed correctly unless you have a mod_rewrite rule setup in Apache.
try setting up your url like this
http://domain/index.php/user/register
and see what happens.
Check this out: http://codeigniter.com/wiki/mod_rewrite/
Bear in mind that what you are doing is this:
mysite.com/kowmanager/user/register
or
mysite.com/index.php/kowmanager/user/register
in either case
1) you are using the knownmanager Directory
2) you are using the controller user
3) you are calling the method 'register'
You can check the following things:
It looks like you are using your user method as a constructor if you are using CI 2 use
function __constructor() {
parent::__constructor();
}
as your constructor.
also you are not calling the view in this controller, are you using a different controller to call the view? I would create a new method called registration_form and call the view from there:
$data['data'] = array();
$this->load->view('view_name', $data);
In this case what you would do is use the following url
mysite.com/kowmanager/index.php/user/registration_form/
Then when the form is submitted it will call the validation method.
I'm not sure if you are loading form_validation before using it
$this->load->library('form_validation');
Good Luck!
Try setting you base_url. If you're working locally and using MAMP or XAMP, it would be something like this:
$config['base_url'] = 'http://localhost/kowmanager';
精彩评论