首页 > 解决方案 > 当我在 localhost 中运行 codeigniter 时,会自动下载 index.php 文件

问题描述

我有一个在线运行良好的 CodeIgniter 项目,需要复制到我的机器上,但是当我这样做并尝试运行时,它的 index.php 文件正在自动下载。

标签: phpcodeignitererror-handling

解决方案


1) 下载最新版本的 CodeIgniter。

2) 解压并将解压后的文件夹粘贴到“htcdocs”目录中。在我的场景中,我使用的是 XAMPP 1.8.1,所以我会将它粘贴到同一目录中。此外,您可以重命名文件夹,例如 CI。

在此处输入图像描述

3)首先查看您的配置文件并进行一些修改。

自动加载.php

$autoload['libraries'] = array('database');
$autoload['helper'] = array('url');

配置文件

$config['base_url'] = 'your localhost url';
in my case:

$config['base_url'] = 'http://localhost/CI/index.php/'; // your current URL on the address bar when displaying the welcome_message
$config['index_page'] = 'index.php'; // page where you want your viewers are redirected when they type in your website name
E.g. base_url — http://www.example.com/ index_page — index.php or straight ahead to news.php, it’s up to you

路由.php

$route['default_controller'] = 'site' // your controller's method, originally "welcome" to display welcome message
I set “site” as the default controller

数据库.php

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = '[your database]'; // e.g. CI_series
$db['default']['dbdriver'] = 'mysql';

提示:如果您还没有访问数据库的任何权限,则默认用户名是 root。另外,暂时将密码留空。

4) 开始使用控制器控制器是应用程序的核心,因为它们决定了应该如何处理 HTTP 请求。控制器只是一个类文件,它以可以与 URI 关联的方式命名。

例如 http://www.example.com/index.php/blog/

在上面的例子中,CodeIgniter 会尝试找到一个名为 blog.php 的控制器并加载它。

当控制器的名称与 URI 的第一段匹配时,它将被加载。

- 参考

现在,让我们输入控制器的代码。

<?php
class Site extends CI_Controller
{
    function index()
    {
        $this->load->view('home.php');
    }
}
?>

基本上,这只会加载我们称为主页的视图/页面

* 什么是负载?

Loader,顾名思义,就是用来加载元素的。这些元素可以是库(类)视图文件、帮助程序、模型或您自己的文件。(参考)

此代码片段将让您显示页面 home.php。此外,由于您调用的是 home.php,因此您必须在 views 文件夹下拥有此页面。创建你的 home.php,写下你想显示的任何东西作为我们第一次运行的测试并保存它。

此代码片段将让您显示页面 home.php。此外,由于您调用的是 home.php,因此您必须在 views 文件夹下拥有此页面。创建你的 home.php,写下你想显示的任何东西作为我们第一次运行的测试并保存它。

主页.php

<p>
My view has been loaded. Welcome!
</p>

另外,将我们的控制器保存在控制器文件夹下,文件名应与您的类名相同。在这种情况下,它应该保存为 site.php。


推荐阅读