php base_url() 函数在 codeigniter 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6449386/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:25:38  来源:igfitidea点击:

base_url() function not working in codeigniter

phpcodeigniter

提问by Sanks R

In my web application using codeigniter. I am trying to use base_url() function but it shows empty results. I have also used autoload helper through autoload file, but then too it doesn't seem to work. Also I had defined base constants but all in vain.

在我的 web 应用程序中使用 codeigniter。我正在尝试使用 base_url() 函数,但它显示空结果。我还通过自动加载文件使用了自动加载助手,但它似乎也不起作用。我也定义了基本常量,但都是徒劳的。

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title><?php echo $title; ?></title>        
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <link rel="stylesheet" href="<?php echo base_url();?>/css/template/default.css" type="text/css" />
        <script type="text/javascript">
            //<![CDATA[
            base_url = '<?= base_url();?>';
            //]]>
        </script>
    </head>

回答by Sampson

In order to use base_url(), you must first have the URL Helper loaded. This can be done either in application/config/autoload.php(on or around line 67):

为了使用base_url(),您必须首先加载 URL Helper。这可以在application/config/autoload.php(第 67 行或附近)中完成:

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

Or, manually:

或者,手动:

$this->load->helper('url');

Once it's loaded, be sure to keep in mind that base_url()doesn't implicitly print or echo out anything, rather it returns the value to be printed:

加载后,请务必记住,base_url()它不会隐式打印或回显任何内容,而是返回要打印的值:

echo base_url();

Remember also that the value returned is the site's base url as provided in the config file. CodeIgniter will accomodate an empty value in the config file as well:

还要记住,返回的值是配置文件中提供的站点的基本 URL。CodeIgniter 也会在配置文件中容纳一个空值:

If this (base_url) is not set then CodeIgniter will guess the protocol, domain and path to your installation.

application/config/config.php, line 13

如果这个 (base_url) 没有设置,那么 CodeIgniter 会猜测你安装的协议、域和路径。

application/config/config.php,第 13 行

回答by prash.patil

If you want to use base_url(), so we need to load url helper.

如果要使用base_url(),那么我们需要加载 url helper。

  1. By using autoload $autoload['helper'] = array('url');
  2. Or by manually load in controller or in view $this->load->helper('url');
  1. 通过使用自动加载 $autoload['helper'] = array('url');
  2. 或者通过在控制器或视图中手动加载 $this->load->helper('url');

Then you can user base_url()anywhere in controller or view.

然后您可以base_url()在控制器或视图中的任何位置使用。

回答by Ravi Mane

First of all load URL helper. you can load in "config/autoload.php" file and add following code $autoload['helper'] = array('url');

首先加载 URL 助手。您可以加载“config/autoload.php”文件并添加以下代码 $autoload['helper'] = array('url');

or in controller add following code

或在控制器中添加以下代码

$this->load->helper('url');

then go to config.php in cofig folder and set

然后转到配置文件夹中的 config.php 并设置

$config['base_url'] = 'http://urlbaseurl.com/';

hope this will help thanks

希望这会有所帮助谢谢

回答by reymundolopez

Check if you have something configured inside the config file /application/config/config.phpe.g.

检查您是否在配置文件中配置了某些内容,/application/config/config.php例如

$config['base_url'] = 'http://example.com/';

回答by Sujeet Kumar

I think you haven't edited codeigniter files to enable base_url(). you try to assign it in url_helper.php you also can do the same config/autoload.php file. you can add this code in your autoload.php

我认为您还没有编辑 codeigniter 文件来启用base_url()。您尝试在 url_helper.php 中分配它,您也可以执行相同的 config/autoload.php 文件。您可以在 autoload.php 中添加此代码

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

Than You will be able to ue base_url() like this

你将能够像这样使用 base_url()

<link rel="stylesheet" href="<?php echo base_url();?>/css/template/default.css" type="text/css" />

回答by pbarney

If you don't want to use the url helper, you can get the same results by using the following variable:

如果您不想使用 url 帮助器,则可以使用以下变量获得相同的结果:

$this->config->config['base_url']

It will return the base url for you with no extra steps required.

它将为您返回基本 url,无需额外的步骤。

回答by Nooha Haris

Load url helper in controller

在控制器中加载 url helper

$this->load->helper('url');

$this->load->helper('url');

回答by Dheeraj Thedijje

Anything if you use directly in the Codeigniter framework directly, like base_url(), uri_string(), or word_limiter(), All of these are coming from some sort of Helperfunction of framework.

任何直接在 Codeigniter 框架中直接使用的东西,比如base_url(), uri_string(), 或者word_limiter(),所有这些都来自于某种Helper框架的功能。

While some of Helpersmay be available globally to use just like log_message()which are extremely useful everywhere, rest of the Helpers are optional and use case varies application to application. base_url()is a function defined in urlhelper of the Framework.

虽然其中一些Helpers可能在全球范围内可用,就像log_message()在任何地方都非常有用一样,其余的 Helpers 是可选的,用例因应用程序而异。base_url()url在框架的助手中定义的函数。

You can learn more about helper in Codeigniter user guide's helper section.

您可以在Codeigniter 用户指南的 helper 部分了解有关 helper 的更多信息

You can use base_url()function once your current class have access to it, for which you needs to load it first.

base_url()一旦您的当前类可以访问它,您就可以使用它,为此您需要先加载它。

$this->load->helper('url')

You can use this line anywhere in the application before using the base_url()function.

在使用该base_url()功能之前,您可以在应用程序的任何位置使用此行。

If you need to use it frequently, I will suggest adding this function in config/autoload.phpin the autoload helperssection.

如果您需要经常使用它,我会建议config/autoload.php在该autoload helpers部分中添加此功能。

Also, make sure you have well defined base_urlvalue in your config/config.phpfile.

另外,请确保您base_urlconfig/config.php文件中有明确定义的值。

This will be the first configuration you will see,

这将是您将看到的第一个配置,

$config['base_url'] = 'http://yourdomain.com/'; 

You can check quickly by

您可以通过以下方式快速检查

echo base_url();

Reference: https://codeigniter.com/user_guide/helpers/url_helper.html

参考:https: //codeigniter.com/user_guide/helpers/url_helper.html

回答by maneesh kumar gautam

Question -I wanted to load my css file but it was not working even though i autoload and manual laod why ? i found the solution => here is my solution : application>config>config.php $config['base_url'] = 'http://localhost/CodeIgniter/'; //paste the link to base url

问题 - 我想加载我的 css 文件,但即使我自动加载和手动加载它也不起作用,为什么?我找到了解决方案 => 这是我的解决方案: application>config>config.php $config['base_url'] = ' http://localhost/CodeIgniter/'; //将链接粘贴到基本网址

question explanation:

问题解释:

" > i had my bootstrap.min.css file inside assets/css folder where assets is root directory which i was created.But it was not working even though when i loaded ? 1. $autoload['helper'] = array('url'); 2. $this->load->helper('url'); in my controllar then i go to my

" > 我的 bootstrap.min.css 文件位于 assets/css 文件夹中,其中 assets 是我创建的根目录。但是即使我加载它也不起作用?1. $autoload['helper'] = array(' url'); 2. $this->load->helper('url'); 在我的控制器中然后我去我的

回答by Asraful Haque

I encountered with this issue spending couple of hours, however solved it in different ways. You can see, I have just created an assets folder outside application folder. Finally I linked my style sheet in the page header section. Folder structure are below images.

我花了几个小时遇到这个问题,但是以不同的方式解决了它。你可以看到,我刚刚在应用程序文件夹之外创建了一个资产文件夹。最后,我在页眉部分链接了我的样式表。文件夹结构如下图。

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

Before action this you should include urlhelper file either in your controller class method/__constructor files or by in autoload.phpfile. Also change $config['base_url'] = 'http://yoursiteurl';in the following file application/config/config.php

在执行此操作之前,您应该在控制器类方法/__constructor 文件或autoload.php文件中包含url帮助程序文件。还要更改以下文件application/config/config.php$config['base_url'] = 'http://yoursiteurl';

If you include it in controller class method/__constructor then it look like

如果将它包含在控制器类方法/__constructor 中,则它看起来像

public function __construct()
{
    $this->load->helper('url');
}

or If you load in autoload file then it would looks like

或者如果您加载自动加载文件,则它看起来像

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

Finally, add your stylesheet file. You can link a style sheet by different ways, include it in your inside section

最后,添加您的样式表文件。您可以通过不同方式链接样式表,将其包含在您的内部部分

-><link rel="stylesheet" href="<?php echo base_url();?>assets/css/style.css" type="text/css" />

-><link rel="stylesheet" href="<?php echo base_url();?>assets/css/style.css" type="text/css" />

-> or

-> 或

<?php

    $main = array(
    'href'       => 'assets/css/style.css',
    'rel'        => 'stylesheet',
    'type'       => 'text/css',
    'title'      => 'main stylesheet',
    'media'      => 'all',
    'index_page' => true
    );


echo link_tag($main); ?>

-> or

-> 或

finally I get more reliable code cleaner concept. Just create a config file, named styles.phpin you application/config/styles.phpfolder. Then add some links in styles.phpfile looks like below

最后我得到了更可靠的代码更清洁的概念。只需在application/config/styles.php文件夹中创建一个名为styles.php 的配置文件。然后在styles.php文件中添加一些链接如下所示

<?php
 $config['style'] = array(
    'main' => array(
        'href'       => 'assets/css/style.css',
        'rel'        => 'stylesheet',
        'type'       => 'text/css',
        'title'      => 'main stylesheet',
        'media'      => 'all',
        'index_page' => true
    )
 );

?>

call/add this config to your controller class method looks like below

将此配置调用/添加到您的控制器类方法如下所示

$this->config->load('styles');
$data['style'] = $this->config->config['style'];

Pass this data in your header template looks like below.

在您的标题模板中传递此数据,如下所示。

$this->load->view('templates/header', $data);

And finally add or link your css file looks like below.

最后添加或链接您的 css 文件,如下所示。

<?php echo link_tag($style['main']); ?>