Codeigniter - 在此服务器上找不到请求的URL

时间:2012-11-24 17:39:56

标签: php codeigniter codeigniter-url

在过去的几天里,我的挫折感已经达到了新的高度。我试图在codeigniter中做一个简单的登录系统,我不能让它正常工作。

提交登录表单时出现以下错误:

在此服务器上找不到请求的URL / wishlist / login_controller / login。

以下是表格:

<form method="post" action="<?php echo base_url()?>login_controller/login">
<input type="text"placeholder="E-mail" id="email" name="email">
<input type="password" placeholder="Password" id="password" name="password">
<input class="btn btn-primary span2" type="submit" id="sign-in" value="Sign In">
</form>

这是我在login_controller中的登录功能:

class Login extends CI_Controller {


function login() {

    $data['error'] = 0;

    if ($_POST) {

        $this->load->model('user_model');
        $email = $this->input->post('email', true);
        $password = $this->input->post('password', true);
        $user = $this->user_model->login($email, $password);

        if (!$user) {
            $data['error'] = 1;
        } else {
            $this->session->set_userdata('userID', $user['userID']);
            $this->session->set_userdata('firstname',    $user['firstname']);

            redirect(base_url().'admin_controller');

        }
    }

    $this->load->view('home_view');
}

我在localhost上使用wampserver。以下是它尝试访问的网址: 本地主机/愿望清单/ login_controller /登录

假设base_url()之后的第一部分是控制器而第二部分是该控制器中的函数,我是否正确?

config文件夹中的几个设置。

$config['base_url'] = '';
$config['index_page'] = 'index.php';
$route['default_controller'] = "site_controller";

4 个答案:

答案 0 :(得分:3)

请从

重命名您的控制器
class Login extends CI_Controller {

class Login_Controller extends CI_Controller {

然后你可以做

<form method="post" action="<?php echo site_url('login_controller/login'); ?>">
<input type="text"placeholder="E-mail" id="email" name="email">
<input type="password" placeholder="Password" id="password" name="password">
<input class="btn btn-primary span2" type="submit" id="sign-in" value="Sign In">
</form>

然后使用它:

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

和htacees:

RewriteEngine On
RewriteBase /wishlist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /wishlist/index.php/$1 [L]

如果没有在路线上切换config.php文件:

| URI PROTOCOL
|--------------------------------------------------------------------------
|
| This item determines which server global should be used to retrieve the
| URI string.  The default setting of 'AUTO' works for most servers.
| If your links do not seem to work, try one of the other delicious flavors:
|
| 'AUTO'            Default - auto detects
| 'PATH_INFO'       Uses the PATH_INFO
| 'QUERY_STRING'    Uses the QUERY_STRING
| 'REQUEST_URI'     Uses the REQUEST_URI
| 'ORIG_PATH_INFO'  Uses the ORIG_PATH_INFO
|
*/
$config['uri_protocol'] = 'AUTO';

我使用AUTO可能需要QUERY_STRING或REQUEST_URI选项

答案 1 :(得分:1)

将login()方法更改为类似user_login的方法,因为对于类使用相同的名称,如果不将该方法用作构造函数,则方法不正确。

尝试像这样访问您的网址

http://localhost/wishlist/index.php/login/user_login

因为你没有改变任何stting而没有添加htaccess文件,你的代码应该是正常可访问的,所以codeiginter有一个访问url的模式,如

http://localhost/folder_name/index.php/controller_name/method_name

似乎您的访问网址不正确。

答案 2 :(得分:1)

您同时遇到两个问题:

  1. 您的表单指向'login_controller',而您的控制器名为'login'(已经回答过)。

  2. 您必须以与控制器相同的方式命名方法。

    此方法(除非您的类在命名空间内,但CodeIgniter不是这种情况)作为类构造函数执行。

答案 3 :(得分:0)

当控制器名称为action="<?php echo base_url()?>login_controller/login">"login"时,您致电method "login"

你需要打电话 base_url('login/login'); 确保在base_url中设置config.php,您已设置index_page。

也许您没有.htaccess,那么您需要致电mysite.com/index.php/login/login/