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

发布于 2024-11-17 00:55:55 字数 661 浏览 0 评论 0原文

在我的网络应用程序中使用 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>

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

り繁华旳梦境 2024-11-24 00:55:55

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

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

或者手动:

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

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

echo base_url();

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

如果未设置此 (base_url),则 CodeIgniter 将猜测您的安装的协议、域和路径。

application/config/config.php,第 13 行

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):

$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:

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:

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

辞慾 2024-11-24 00:55:55

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

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

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

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

  1. By using autoload $autoload['helper'] = array('url');
  2. Or by manually load in controller or in view $this->load->helper('url');

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

苍景流年 2024-11-24 00:55:55

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

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

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

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

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

希望这会有所帮助
谢谢

First of all load URL helper. you can load in "config/autoload.php" file and add following code
$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['base_url'] = 'http://urlbaseurl.com/';

hope this will help
thanks

简美 2024-11-24 00:55:55

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

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

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

$config['base_url'] = 'http://example.com/';
梦幻之岛 2024-11-24 00:55:55

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

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

,然后你就可以像这样使用 base_url()

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

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

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

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

<link rel="stylesheet" href="<?php echo base_url();?>/css/template/default.css" type="text/css" />
无人问我粥可暖 2024-11-24 00:55:55

除了确保您已设置 config/autoload.php:

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

将 application/config/config.php 更改为:

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

成为动态基本 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']);

并使用 php 中的主机在本地运行它,下面只是一个示例端口。

php -S localhost:2000

Apart from making sure you have set config/autoload.php:

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

Change application/config/config.php from:

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

Become a dynamic 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']);

And using the host from php to run it on local, below is just an example port.

php -S localhost:2000
谜兔 2024-11-24 00:55:55

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

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

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

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

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

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

滥情空心 2024-11-24 00:55:55

在控制器中加载 url 助手

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

Load url helper in controller

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

长伴 2024-11-24 00:55:55

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

虽然某些 Helpers 可以全局使用,就像 log_message() 一样在任何地方都非常有用,但其余的 Helpers 是可选的,并且用例因应用程序而异。 base_url() 是框架的 url 帮助器中定义的函数。

您可以在 Codeigniter 用户指南的帮助程序部分了解有关帮助程序的更多信息。

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

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

在使用 base_url() 函数之前,您可以在应用程序中的任何位置使用此行。

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

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

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

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

您可以通过参考快速检查

echo base_url();

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

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 Helper function of framework.

While some of Helpers may 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 url helper of the Framework.

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

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

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

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

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

Also, make sure you have well defined base_url value in your config/config.php file.

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

我的鱼塘能养鲲 2024-11-24 00:55:55

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

问题解释:

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

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

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

杀お生予夺 2024-11-24 00:55:55

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

输入图片此处描述
输入图片此处描述

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

如果您将其包含在控制器类中, method/__constructor 那么它看起来像

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

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

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

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

->

->或

<?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); ?>

->或者

最终我得到了更可靠的代码清理概念。只需在 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
    )
 );

?>

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

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

在标头模板中传递此数据如下所示。

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

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

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

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 here
enter image description here

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

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

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" />

-> 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.php in you application/config/styles.php folder.
Then add some links in styles.php file looks like below

<?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.

<?php echo link_tag($style['main']); ?>
来世叙缘 2024-11-24 00:55:55

所有直接访问的函数将仅通过帮助程序类加载。

像 URL、安全性、文件都是帮助器,您还可以加载自定义帮助器。

配置/自动加载.php

$autoload['helper'] = array('url', 'file', 'authorization', 'custom');

This All Directly Access Function Will Be Loaded Through Helper Class Only.

Like URL, Security, File all Are Helpers and You can Also Load Custom Helpers.

config/autoload.php

$autoload['helper'] = array('url', 'file', 'authorization', 'custom');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文