Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CodeIgniter base_url(),localhost中的链接为我提供了到live server的链接_Codeigniter_Url_Hyperlink_Base Url - Fatal编程技术网

CodeIgniter base_url(),localhost中的链接为我提供了到live server的链接

CodeIgniter base_url(),localhost中的链接为我提供了到live server的链接,codeigniter,url,hyperlink,base-url,Codeigniter,Url,Hyperlink,Base Url,我在本地主机的CodeIgniter中处理网页,但该网页也在live server中 它对所有链接使用base\u url() 例如: <link rel="stylesheet" href="<?= base_url() ?>assets/style.css"> <script src="<?= base_url() ?>assets/jquery-ui.min.js"></script> ... <a href="<?=

我在本地主机的CodeIgniter中处理网页,但该网页也在live server中

它对所有链接使用
base\u url()

例如:

<link rel="stylesheet" href="<?= base_url() ?>assets/style.css">
<script src="<?= base_url() ?>assets/jquery-ui.min.js"></script>
...
<a href="<?= base_url() ?>show/edit/other">

您可以将下面的代码设置为config.php,它将采用relavent url如果您在localhost中,它将采用localhost,如果您在live server中,它将采用live url

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

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

您可以在
application/config
中创建两个文件夹,一个用于生产(live server),一个用于开发(localhost)

  • 开发=>application/config/development
  • 生产=>application/config/production
然后复制主
应用程序/config
文件夹中的任何文件,并将其粘贴到
开发
生产
文件夹中

application/config/development/config.php

$config['base_url'] = 'http://localhost';
$config['base_url'] = 'http://example.com';
application/config/production/config.php

$config['base_url'] = 'http://localhost';
$config['base_url'] = 'http://example.com';
根据
index.php
中的ENV常量,它将从生产或开发文件夹加载设置。因此,如果ENV=='development',将使用
application/config/development
中的设置

如果您想要资产的相对路径,请将其添加到html的
标题中

<base href="<?php echo base_url();?>" />

无需触摸config.php。遵循此解决方案,即使通过htaccss删除index.php,它也可以在本地和实时环境下工作

要添加文件(css、图像、js等),请使用以下命令

  <?php echo base_url('assets/js/test.js');?>
   <?php echo site_url('controller/metho_name');?>

对于其他内容,请使用以下内容

  <?php echo base_url('assets/js/test.js');?>
   <?php echo site_url('controller/metho_name');?>

它将在live或本地服务器中自动处理,并保持config.php不变


谢谢

我应该用application/config/config.php文件删除它吗?如果您在
development
production
文件夹中都有它的副本,您可以删除它。您可以对
database.php执行相同的操作。好的,我照您说的做了,但是我的链接显示为不存在的链接,例如,它看起来像。您需要将它设置为localhost
$config['base\u url']='http://localhost/“
或者如果您在本地工作,您可以将其保留为空
$config['base\u url']=”
,您需要确保
ENV=='development'
index.php
内部。最后,问题是.htaccess中的CI与RewriteBase一起出现。我将其设置为off,检查
APPPATH中的
$config['base\u url']
config/config'。EXT
设置得很好。编辑:没有看到Phillip的答案…定义基本url的最佳方法我一直面临一个问题,只有这个解决方案解决了我的问题,我希望我能给你100张赞成票:)