php CodeIgniter 无法在本地主机上工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16980749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter not working on localhost
提问by tara giska
I downloaded a website from ftp . The website is made with CodeIgniter. My problem is it doesn't seem to work on localhost.
我从 ftp 下载了一个网站。该网站是使用 CodeIgniter 制作的。我的问题是它似乎不适用于本地主机。
Base URL in config.php:
config.php 中的基本 URL:
$config['base_url'] = 'http://sample_localhost/mysite/';
Settings in database.php:
database.php 中的设置:
$db['default']['hostname'] = 'sample_localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'admin';
$db['default']['database'] = 'my_database';
Any help is very much appreciated. Thanks in advance!
很感谢任何形式的帮助。提前致谢!
回答by Gaurang P
1)Download the latest version of CodeIgniter.
1)下载最新版本的CodeIgniter。
2)Extract it and paste the extracted folder at the ‘htcdocs' directory. In my scenario, I am using XAMPP 1.8.1, so I will paste it on the same directory. Also, you can rename the folder E.g. CI.
2)解压并将解压后的文件夹粘贴到“htcdocs”目录中。在我的场景中,我使用的是 XAMPP 1.8.1,因此我会将其粘贴到同一目录中。此外,您可以重命名文件夹,例如 CI。
3)Take a look first at your config files and made some few modifications.
3)首先查看您的配置文件并进行一些修改。
autoload.php
自动加载.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
例如 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
I set “site” as the default controller
我将“站点”设置为默认控制器
database.php
数据库.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:Username as a default would be root if you don't have any permissions for accessing the database yet. Also, leave password blank for now.
提示:如果您还没有任何访问数据库的权限,则默认用户名将是 root。另外,暂时将密码留空。
4)Start working with the Controllers Controllers are the heart of your application, as they determine how HTTP requests should be handled. A Controller is simply a class file that is named in a way that can be associated with a URI.
4)开始使用控制器控制器是应用程序的核心,因为它们决定了 HTTP 请求的处理方式。控制器只是一个以可以与 URI 关联的方式命名的类文件。
E.g.
例如
http://www.example.com/index.php/blog/
In the above example, CodeIgniter would attempt to find a controller named blog.php and load it.
在上面的示例中,CodeIgniter 将尝试查找名为 blog.php 的控制器并加载它。
When a controller's name matches the first segment of a URI, it will be loaded.
当控制器的名称与 URI 的第一段匹配时,它将被加载。
–参考
Now, let's type the code for our controller.
现在,让我们输入控制器的代码。
<?php
class Site extends CI_Controller
{
function index()
{
$this->load->view('home.php');
}
}
?>
Basically, this will just load our view/page called home
基本上,这只会加载我们称为home 的视图/页面
* What is load?
* 什么是负载?
Loader, as the name suggests, is used to load elements. These elements can be libraries (classes) View files, Helpers, Models, or your own files. (ref)
Loader,顾名思义,就是用来加载元素的。这些元素可以是库(类)视图文件、助手、模型或您自己的文件。(参考)
This code snippet would let you display the page, home.php. Also since, you're calling home.php, you must have this page under the views folder. Create your home.php, write anything you would like to display as a test for our first run and save it.
此代码片段可让您显示页面 home.php。此外,由于您正在调用 home.php,因此您必须在 views 文件夹下拥有此页面。创建您的 home.php,编写您想显示的任何内容作为我们第一次运行的测试并保存它。
This code snippet would let you display the page, home.php. Also since, you're calling home.php, you must have this page under the views folder. Create your home.php, write anything you would like to display as a test for our first run and save it.
此代码片段可让您显示页面 home.php。此外,由于您正在调用 home.php,因此您必须在 views 文件夹下拥有此页面。创建您的 home.php,编写您想显示的任何内容作为我们第一次运行的测试并保存它。
home.php
主页.php
<p>
My view has been loaded. Welcome!
</p>
Also, save our controller under the controllersfolder, file name should be the same as your class name. In this case, it should be saved as site.php.
另外,将我们的控制器保存在控制器文件夹下,文件名应与您的类名相同。在这种情况下,它应该保存为site.php。
The first run:
第一次运行:
回答by Gautam3164
My be it should be like
我应该是这样
$config['base_url'] = 'http://localhost/mysite/';
or like
或喜欢
$config['base_url'] = 'http://MY_IP/mysite/';
And check your index value in config file like
并在配置文件中检查您的索引值,例如
$config['index_page'] = 'index.php';
if it is so then you need to add index.php in your url before the controller name and check your folder has permissions also...and at your router.php see this
如果是这样,那么您需要在控制器名称之前的 url 中添加 index.php 并检查您的文件夹是否也有权限......并在您的 router.php 看到这个
$route['default_controller'] = "welcome";
you can change if you want to display a controller by defaulty when you call sample_localhost/my_site
您可以更改是否要在调用时默认显示控制器 sample_localhost/my_site
回答by Nitin Karale
Replace base url with following code will run & get speed
用以下代码替换基本网址将运行并获得速度
$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']);
回答by DiegoSoto
The solution I've implemented, works with different scenarios that have problems on local host: HTTP 404 error, Windows 10 [::1] issue, Session not working on localhost issue, session class not working in Chrome issue.
我实施的解决方案适用于在本地主机上有问题的不同场景:HTTP 404 错误、Windows 10 [::1] 问题、会话无法处理本地主机问题、会话类无法在 Chrome 问题中工作。
In local host environment:
在本地主机环境中:
1. Do not use .htaccess, remove or rename the file.
1. 不要使用 .htaccess,删除或重命名文件。
2. In config.php, make explicit the base url and the index page.
2. 在 config.php 中,明确基础 url 和索引页面。
// Base Site URL
$config['base_url'] = 'http://localhost/';
// Index File
$config['index_page'] = 'index.php/'; // Note backslash at the end
3. On the Controller, each time you invoke a method, use site_url()function instead base_url(). You should have loaded the url_helper. See too.
3. 在 Controller 上,每次调用方法时,使用site_url()函数代替base_url()。您应该已经加载了url_helper。见 也。
redirect(site_url('entity/method'));
That's all.
就这样。
Views example:
视图示例:
<li><a href="<?php echo site_url('entity/method')?>">Method</a></li>
Less clean solution: always concatenate the index_page()with base_url(). Do not forget to ensure the backslash before method.
不太干净的解决方案:始终将index_page()与base_url() 连接起来。不要忘记在方法之前确保反斜杠。
redirect(base_url().index_page().'entity/method');
Explanation
解释
Typically in production environment, you will leave empty a index_pageparameter, perhaps base_urlparameter too, and will use the ".htaccess" file. Then, site_url()function or alternative base_url()+index_page()will return a recognizable correct address.
通常在生产环境中,您会将index_page参数留空,也可能是base_url参数,并将使用“.htaccess”文件。然后,site_url()函数或替代base_url()+index_page()将返回一个可识别的正确地址。
回答by Dejan Dozet
I had it working on the localhost, but I wanted to make it like that so I can easily upload it to the hosting server.
我让它在本地主机上工作,但我想让它像这样我可以轻松地将它上传到托管服务器。
And my solution is this:
我的解决方案是这样的:
$config['base_url'] = ($_SERVER['HTTP_HOST'] === 'localhost' ? '/programiranje' : '/') ;
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
.htaccess:
.htaccess:
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php/
I am hosting it locally on windows 10 machine in C:\Apache24\htdocs folder.
我将它本地托管在 Windows 10 机器上的 C:\Apache24\htdocs 文件夹中。