【问题标题】:codeigniter can only load index(), not any other methodscodeigniter 只能加载 index(),不能加载任何其他方法
【发布时间】:2014-11-10 17:39:35
【问题描述】:

我在使用 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>

非常感谢!

【问题讨论】:

  • 你的这个网址有效吗? www.example.com/index.php?/user/test

标签: php html css apache codeigniter


【解决方案1】:

您需要更改您的 .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';

【讨论】:

  • 我更改了 Web 应用程序根文件夹中的 .htaccess 文件,它不起作用...
  • 嗨,它确实有效,但是 $config['index_page'] = 'Index.php';
【解决方案2】:

您的代码中有几件事引起了人们的注意:

首先,使用var关键字。用于在php4中声明类成员。

在 php5 及更高版本中已弃用。

它会起作用的。但是提出警告是一种不好的做法。

第二。从您发布的代码中,我相信您还没有自动加载 Session 库。

将构造函数中的 session_start() 代码更改为:

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

我注意到的第三件事是这行代码:

$this->load->view('banner','Login');

loader 类的视图函数接受以下格式的参数。

$this->load->view('file_name', $data, true/false);

您的语法可能不正确。

如果您打算加载横幅和登录文件,您应该这样做:

$this->load->view('banner');
$this->load->view('Login');

附带说明,使用原生 php 会话和 CI 会话可能会造成混淆,并且在某些情况下还可能引发冲突。

【讨论】:

    猜你喜欢
    • 2021-10-24
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多