Codeigniter提供了错误的网址

时间:2016-08-14 01:05:15

标签: php html codeigniter

这些是我的Codeigniter代码。我正处于极品阶段,所以请耐心等待。

在我的控制器文件夹中:Users.php

<?php  

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Controller {

function __construct()  {  
parent::__construct();  
$this->load->helper('url');  
$this->load->model('Users_model');
}

public function index()  {  
    $data['user_list'] = $this->Users_model->get_all_users();  
    $this->load->view('Show_users', $data);  
} 


public function add_form()  {  
    $this->load->view('Insert');  
}

public function insert_new_user()  {  
    $udata['name'] = $this->input->post('name');  
    $udata['email'] = $this->input->post('email');  
    $udata['address'] = $this->input->post('address'); 
    $udata['mobile'] = $this->input->post('mobile');  
    $res = $this->users_model->insert_users_to_db($udata);  
    if($res){  
        header('location:'.base_url()."index.php/Users/".$this->index());  
    }  
} 
}

?>

在我的模型文件夹中:Users_model.php

<?php  

class Users_model extends CI_Model {

function __construct()  {  
    parent::__construct();  
    $this->load->database();  
}

public function get_all_users()  {  
    $query = $this->db->get('users');  
    return $query->result();  
}

public function insert_users_to_db($data) {
    return $this->db->insert('users', $data);
}
}   

?>

在我的views文件夹中:Show_users.php

<html>
<head>
    <meta charset="UTF-8">
    <title>CI CRUD</title>
    <script type="text/javascript">
        function show_confirm(act,gotoid)  {  
            if(act=="edit")  
                var r=confirm("Do you really want to edit?");  
            else  
                var r=confirm("Do you really want to delete?");  
            if (r==true)  {  window.location="<?php //echo base_url();?>index.php/Users/"+act+"/"+gotoid;  
            }  
        }
    </script>
</head>
<body>
    <h2> Simple CI CRUD Application </h2>  
    <table width="600" border="1" cellpadding="5">  
        <tr>  
            <th scope="col">Id</th>  
            <th scope="col">User Name</th>  
            <th scope="col">Email</th>  
            <th scope="col">Address</th>  
            <th scope="col">Mobile</th>  
        </tr>  
        <?php foreach ($user_list as $u_key){ ?>  
        <tr>  
            <td><?php echo $u_key->id; ?></td>  
            <td><?php echo $u_key->name; ?></td>  
            <td><?php echo $u_key->email; ?></td>  
            <td><?php echo $u_key->address; ?></td>  
            <td><?php echo $u_key->mobile; ?></td>  
            <td width="40" align="left" ><a href="#" onClick="show_confirm('edit',<?php echo $u_key->id;?>)">Edit</a>
            </td>  
            <td width="40" align="left" ><a href="#" onClick="show_confirm('delete',<?php echo $u_key->id;?>)">Delete </a>
            </td>
        </tr>  
        <?php }?>  
        <tr>
            <td colspan="7" align="right"> <a href="<?php echo base_url(); ?>index.php/Users/add_form">Insert New User</a></td>
        </tr>
    </table>
</body>
</html>

也是这个:Insert.php

<html>
<head>
    <meta charset="UTF-8">
    <title>CI Insert Form</title>
</head>
<body>
    <form method="post" action="<?php echo base_url();?>index.php/Users/insert_user_db">  
        <table width="400" border="0" cellpadding="5">  
            <tr>  
                <th width="213" align="right" scope="row">Enter your username</th>  
                <td width="161"><input type="text" name="name" size="20" /></td>  
            </tr>  
            <tr>  
                <th align="right" scope="row">Enter your email</th>  
                <td><input type="text" name="email" size="20" /></td>  
            </tr>  
            <tr>  
                <th align="right" scope="row">Enter your Mobile</th>  
                <td><input type="text" name="mobile" size="20" /></td>  
            </tr>  
            <tr>  
                <th align="right" scope="row">Enter Your Address</th>  
                <td><textarea name="address" rows="5" cols="20"></textarea></td>  
            </tr>  
            <tr>  
                <th align="right" scope="row">&nbsp;</th>  
                <td><input type="submit" name="submit" value="Send" /></td>  
            </tr>  
        </table>  
    </form>
</body>
</html>

除了点击

之外,所有这些代码都能正常工作
Insert New User 

我的代码应该带我到http://127.0.0.1/ci_beginning/index.php/users/add_form **请注意“用户”中的“s”,因为我已经在<td colspan="7" align="right"> <a href="<?php echo base_url(); ?>index.php/Users/add_form">Insert New User</a></td>

上表明了这一点

它让我这样做:http://127.0.0.1/ci_beginning/index.php/user/add_form

为了让我到add_form向我展示Insert.php视图,我需要转到地址栏并将“s”插入“user”,这样我就不会发现Page not found错误

请指出我错在哪里。感谢您的耐心等待。

1 个答案:

答案 0 :(得分:1)

您需要在CI3中设置base_url

/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
|   http://example.com/
|
| WARNING: You MUST set this value!
|
| If it is not set, then CodeIgniter will try guess the protocol and path
| your installation, but due to security concerns the hostname will be set
| to $_SERVER['SERVER_ADDR'] if available, or localhost otherwise.
| The auto-detection mechanism exists only for convenience during
| development and MUST NOT be used in production!
|
| If you need to allow multiple domains, remember that this file is still
| a PHP script and you can easily do that on your own.
|
*/

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

其他明智的方法是在网址中显示ip

在你的config.php上你可以添加

if (isset($_SERVER['HTTP_HOST'])) {

    $HTTP_SERVER = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
    $HTTP_SERVER .= '://'. $_SERVER['HTTP_HOST'];
    $HTTP_SERVER .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);

} else {

    $HTTP_SERVER = 'http://localhost/';
}

define('HTTP_SERVER', $HTTP_SERVER);

unset($HTTP_SERVER);

$config['base_url'] = HTTP_SERVER;