PHP:404具有CodeIgniter控制器功能

时间:2014-07-09 04:14:22

标签: php codeigniter

在学习CodeIgniter时,我在跟踪this tutorial时遇到障碍。在调用“login_validation”类控制器的“Main”函数时,我找不到404。

  

在此服务器上找不到请求的URL / ci / main / login_validation。

“主”控制器类和函数

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

class Main extends CI_Controller {

    public function index() {
        $this->login();
    }

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

    public function login_validation() {   // NOT FOUND?
        $this->load->library('form_validation');
        $this->form_validation->set_rules('email','Email','required');
        $this->form_validation->set_rules('password','Password','required|md5'); //md5 value encryption

        echo "ok"; //check if function executes

        if ($this->form_validation->run()) {
            redirect('main/members');
        } else {
            $this->load->view('login');
        }
    }

}

View PHP文件中的相关代码

<?php 

    echo form_open('main/login_validation');  // NOT FOUND?

    echo validation_errors();

    echo "<p>Email: ";
    echo form_input('email');
    echo "</p>";

    echo "<p>Password: ";
    echo form_password('password');
    echo "</p>";

    echo "<p>";
    echo form_submit('login_submit','Login');
    echo "</p>";

    echo form_close();

?>

.htaccess代码

目录位于localhost

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /www/ci/

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

ErrorDocument 404 /index.php
</IfModule> 

2 个答案:

答案 0 :(得分:2)

使用.htaccess下面的一个代码

Options +FollowSymLinks
#IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php

答案 1 :(得分:2)

DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]