我今天开始使用CodeIgniter,我正在学习phpacademy的初学者教程。现在我遇到了一个奇怪的问题,我得到了以下非常简单的控制器:
<?php if ( ! defined('BASEPATH')) exit("No direct script access allowed");
class Site extends CI_Controller {
public function index()
{
}
public function home()
{
$this->load->view("view_home", $data);
}
function about()
{
$data['title'] = "About!";
$this->load->view("view_about", $data);
}
function test()
{
echo "test";
}
}
它总是很好地加载索引函数。当我测试它时,它会回显我想要它回显的任何内容。但是当我调用其他函数时,什么也没发生。
我还没有设置我的.htacces,但是当我访问以下地址时:本地主机:8899/索引.php/站点/首页
它不加载 Home 函数。 其他函数("关于"和"测试")也是如此;
当我从 index() 函数中调用其中一个函数("home"、"about"和"test")时,它确实会如何调用它:
class Site extends CI_Controller {
public function index()
{
$this->home();
}
public function home()
{
$this->load->view("view_home", $data);
}
我不确定这里出了什么问题。希望有人能引导我朝着正确的方向前进!
我的路线:
<?php if ( ! defined('BASEPATH')) exit("No direct script access allowed");
$route['default_controller'] = "site";
$route['404_override'] = '';
如果有人因为上述解决方案不起作用而遇到这种情况,好吧,您可以尝试以下步骤,我得到了工作(xampp 本地):
- 将
config.php
中的base_url
更改为http://localhost/application-name/
- 清空
index_page
(默认值应为index.php
) - 转到应用程序的根文件夹,然后检查是否有
index.php
-
在那里制作
.htaccess
文件并写入这些文件:RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]
我会确保控制器类有一个构造函数来加载基本的代码点火器基类。
function __construct()
{
parent::__construct();
}
在每个控制器类中,告诉您的类使用代码点火器系统。 如果不存在,它将不知道如何加载这些函数。
为了清楚起见,请确保它通过 url/site/about 等调用其他方法......另外,您的开发服务器上是否显示 php 错误?