控制器动作不正确

时间:2011-08-11 16:29:53

标签: php codeigniter

我是新手代码点火器,但我一直在观看很多youtube视频,但我开始掌握它的基础知识,但是在我的注册表单上进行测试后,它会转到白页在此服务器上找不到请求的URL / kowmanager / user / register。我不知道为什么。有什么想法吗?

控制器:     

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;
    }
}

}
?>

查看页面:

<!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="submit">

    <?php echo form_submit(array('name' => 'register'), 'Register'); ?>

</dl>

<?php echo form_close(); ?>

</body>

</html>

编辑:

这是我的新代码,它仍在做同样的事情。

<?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;
}

}
?>

还有其他想法吗?

3 个答案:

答案 0 :(得分:2)

默认情况下,CodeIgniter路由的结构如下。

http://example.com/index.php/Controller/Function

如果您的代码中没有'index.php',除非您在Apache中设置了mod_rewrite规则,否则它将无法正确路由。

尝试像这样设置您的网址

http://domain/index.php/user/register

看看会发生什么。

检查一下: http://codeigniter.com/wiki/mod_rewrite/

答案 1 :(得分:2)

请记住,你正在做的是:

mysite.com/kowmanager/user/register 要么 mysite.com/index.php/kowmanager/user/register

在任何一种情况下

1)您正在使用 knownmanager 目录

2)您正在使用控制器用户

3)您正在调用方法'注册'

您可以查看以下内容:

如果您使用CI 2使用

,看起来您正在使用您的用户方法作为构造函数
function __constructor() {
  parent::__constructor();
}

作为你的构造函数。

你也没有在这个控制器中调用视图,你使用不同的控制器来调用视图吗?我将创建一个名为registration_form的新方法,并从那里调用视图:

$data['data'] = array();
$this->load->view('view_name', $data); 

在这种情况下,您要做的是使用以下网址

mysite.com/kowmanager/index.php/user/registration_form/

然后在提交表单时,它将调用验证方法。

我不确定你在使用之前是否加载了form_validation

$this->load->library('form_validation');

祝你好运!

答案 2 :(得分:2)

尝试设置base_url。如果你在本地工作并使用MAMP或XAMPP,它将是这样的:

$config['base_url'] = 'http://localhost/kowmanager';