【问题标题】:When I run codeigniter in localhost then index.php file download automatically当我在 localhost 中运行 codeigniter 时,会自动下载 index.php 文件
【发布时间】:2023-03-19 11:25:01
【问题描述】:

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

【问题讨论】:

  • 您的网络服务器配置有问题。您正在使用什么堆栈以及您的配置是什么?需要更多信息。
  • 对我来说,重命名项目名是解决方案

标签: php codeigniter error-handling


【解决方案1】:

1) 下载最新版本的 CodeIgniter。

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

在此处输入图片说明

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

autoload.php

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

config.php

$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

routes.php

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

database.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,顾名思义,就是用来加载元素的。这些元素可以是库(类)视图文件、帮助程序、模型或您自己的文件。 (参考)

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

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

home.php

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

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

【讨论】:

    【解决方案2】:

    您没有启用/安装 PHP 处理器|模块。

    【讨论】:

      猜你喜欢
      • 2014-04-12
      • 2011-01-27
      • 2017-05-16
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多