首页 文章

CodeIgniter不在localhost上工作

提问于
浏览
5

我从ftp下载了一个网站 . 该网站由CodeIgniter制作 . 我的问题是它似乎不适用于localhost .

config.php中的基本URL:

$config['base_url'] = 'http://sample_localhost/mysite/';

database.php中的设置:

$db['default']['hostname'] = 'sample_localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'admin';
$db['default']['database'] = 'my_database';

很感谢任何形式的帮助 . 提前致谢!

5 回答

  • 0

    我应该是这样的

    $config['base_url'] = 'http://localhost/mysite/';
    

    或者喜欢

    $config['base_url'] = 'http://MY_IP/mysite/';
    

    并检查配置文件中的索引值

    $config['index_page'] = 'index.php';
    

    如果是这样,那么你需要在控制器名称前的url中添加index.php并检查你的文件夹是否也具有权限......并在你的router.php上看到这个

    $route['default_controller'] = "welcome";
    

    如果要在调用 sample_localhost/my_site 时默认显示控制器,可以更改

  • 5

    我有它在localhost上工作,但我想这样做,所以我可以轻松地将其上传到托管服务器 .

    我的解决方案是这样的:

    $config['base_url'] = ($_SERVER['HTTP_HOST'] === 'localhost' ? '/programiranje' : '/') ;
    $config['index_page'] = '';
    $config['uri_protocol'] = 'REQUEST_URI';
    

    的.htaccess:

    RewriteEngine On
    #RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) index.php/$1
    

    我在C:\ Apache24 \ htdocs文件夹中的Windows 10机器上本地托管它 .

  • 9

    1) 下载最新版本的CodeIgniter .

    2) 解压缩并将解压缩的文件夹粘贴到'htcdocs'目录中 . 在我的场景中,我使用XAMPP 1.8.1,所以我将它粘贴在同一目录中 . 此外,您可以重命名文件夹E.g. CI .

    enter image description here

    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或直接发送到news.php,这取决于你

    routes.php

    $route['default_controller'] = 'site' // your controller's method, originally "welcome" to display welcome message
    

    我将“site”设置为默认控制器

    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';
    

    TIP: 如果您还没有访问数据库的任何权限,则默认用户名为root . 此外,暂时将密码留空 .

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

    E.g.

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

    在上面的示例中,CodeIgniter将尝试查找名为blog.php的控制器并加载它 .

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

    Now ,让我们输入控制器的代码 .

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

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

    *** What is load?**

    顾名思义,Loader用于加载元素 . 这些元素可以是库(类)查看文件,助手,模型或您自己的文件 . (ref)

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

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

    home.php

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

    另外,将我们的控制器保存在 controllers 文件夹下, file name should be the same as your class name . 在这种情况下,它应保存为 site.php .

    The first run:

    enter image description here

  • 2

    用以下代码替换基本URL将运行并获得速度

    $config['base_url'] = '';
    $config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
    $config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
    $config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
    
  • 0

    我实现的解决方案适用于在本地主机上出现问题的不同场景:HTTP 404错误,Windows 10 [:: 1]问题,Session无法解决localhost问题,会话类无法在Chrome问题中运行 .

    在本地主机环境中:

    1. Do not use .htaccess ,删除或重命名该文件 .

    2. In config.php ,明确基本网址和索引页面 .

    // Base Site URL
    $config['base_url'] = 'http://localhost/';
    
    // Index File
    $config['index_page'] = 'index.php/';    // Note backslash at the end
    

    3. On the Controller ,每次调用方法时,请使用 site_url() 函数而不是base_url() . 你应该加载url_helper . 见too .

    redirect(site_url('entity/method'));
    

    就这样 .

    视图示例:

    <li><a href="<?php echo site_url('entity/method')?>">Method</a></li>
    

    不太干净的解决方案:始终将index_page()与base_url()连接起来 . 不要忘记在方法之前确保反斜杠 .

    redirect(base_url().index_page().'entity/method');
    

    Explanation

    通常在 生产环境 环境中,您将留空index_page参数,也可能是base_url参数,并将使用".htaccess"文件 . 然后,site_url()函数或替代base_url()index_page()将返回可识别的正确地址 .

相关问题