php - codeigniter 只能加载 index(),不能加载任何其他方法

标签 php html css apache codeigniter

我在使用 codeigniter 时遇到了这个问题,它完美地加载了 index() 页面,但是当涉及到任何其他函数时,它只会抛出 404 错误。这是一个框架代码:

<?php if( ! defined('BASEPATH')) exit('No direct script access alloowed');
Class User extends CI_Controller{
    var $err;

    public function construct(){
        parent::_construct();
        session_start();
        $this->load->database();            
        $this->load->model('User_Model');           
    }

    public function index(){
        if(isset($this->session->userdata['userID'])){
            $this->welcome();
        }else{
            $this->load->view('banner','Login');
            if(isset($this->err)){
                $data["error"] = $this->err;
                $this->load->view('login',$data);
            }else{
                $this->load->view('login');
            }               
            $this->load->view('footer');
        }
    }

    public function test(){
        echo "Hello World";
    }

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

    public function register(){
        $this->load->view('banner','Login');
        if(isset($this->err)){
            $data["error"] = $this->err;
            $this->load->view('register',$data);
        }else{
            $this->load->view('register');
        }
        $this->load->view('footer');
    }

    public function registration(){
        //registration ops
    }

    public function login(){
        //login ops
}
    }
?>

因此,当我尝试导航到 www.example.com/index.php/user 时,它运行完美,但是当我尝试让它运行注册或进行登录操作时,它只会抛出 404 错误.有什么解决办法吗?

这是使用 index() 生成的 html 代码:

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

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

<link href="http://localhost/baseball/css/login.css" rel="stylesheet" type="text/css" />

</head>



<body>

<div>

  <p><img src="http://thumbs.dreamstime.com/z/cartoon-baseball-player-vector-illustration-30999087.jpg" height="250" alt=""/>Baseball Card Shop</p>

  <hr />

  <p class="loginInfo">

        <a href="shopping cart">Shopping Cart</a>

  </p>

  <hr/>

</div>

 <div class="central_login">

    <div class="main">

        <form action="http://localhost/baseball/index.php/user/login" method="post" accept-charset="utf-8">     <div class="left">

          <p>

            <label for="login"> Login: </label>          

            <input type="text" name="login" id="login" pattern="^[a-zA-Z0-9]+$" required/>

          </p>

          <p>

            <label for="pwd">Password: </label>

            <input type="password" name="pwd" id="pwd" required/>

          </p>

          <p><label id='pwdErr'></label>

            <input type="submit" value="Log in" id="create-account" class="button"/>

        </p>

        </div>

         </form>     <div class="right">

        <form action="http://localhost/baseball/index.php/user/register" method="post" accept-charset="utf-8">          <p>Not our customer, worry not ! Register here, it is free!</p>

            <input type="submit" value="Register HERE!" id="register" class="button"/> 

        </form>  </div>

         </div>

</div>

<footer>

  <p>CSC309 Assignment</p>

  <p>Baseball Web Application</p>

</footer>

</body>

</html>

非常感谢!

最佳答案

您需要更改您的 .htaccess。以下 .htaccess 将帮助您:

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

并且需要在config.php中更改如下配置:

$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';

关于php - codeigniter 只能加载 index(),不能加载任何其他方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26849623/

相关文章:

html - 如何创建具有全屏尺寸的 CSS 形状

html - 如何将第一个表格列设置为粗体(CSS)

javascript - 如何从代码隐藏中删除 Html <li> 元素符号?

PHP fatal error : Out of memory - Memory Allocation Strategies for Big Data

php - 如何在GAE中向网络浏览器显示php错误?

php - 如何在PHP中将键值添加到解码的json中?

html - Glyphicon 在 Prestashop 1.6 中不显示

php - 将 PHP 与 Linux 服务器上的 Paradox .db 文件连接

html - 使用 html 表格时,在 CSS3 多列中强制分栏

php - 是否可以创建网页打印 View 的 PDF 文件?