base_url() 函数在 codeigniter 中不起作用
在我的网络应用程序中使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
为了使用
base_url()
,您必须首先加载 URL Helper。这可以在application/config/autoload.php
中完成(在第 67 行或附近):或者手动:
加载后,请务必记住
base_url()< /code> 不会隐式打印或回显任何内容,而是返回要打印的值:
还请记住,返回的值是配置文件中提供的站点的基本 url。 CodeIgniter 还将在配置文件中容纳一个空值:
In order to use
base_url()
, you must first have the URL Helper loaded. This can be done either inapplication/config/autoload.php
(on or around line 67):Or, manually:
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: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:
如果你想使用
base_url()
,那么我们需要加载url helper。$autoload['helper'] = array('url');
$this->load->helper('url ');
然后您可以在控制器或视图中的任何位置使用
base_url()
。If you want to use
base_url()
, so we need to load url helper.$autoload['helper'] = array('url');
$this->load->helper('url');
Then you can user
base_url()
anywhere in controller or view.首先加载 URL 助手。您可以加载“config/autoload.php”文件并添加以下代码
$autoload['helper'] = array('url');
或在控制器中添加以下代码
,然后转到 cofig 文件夹中的 config.php 并设置
希望这会有所帮助
谢谢
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
then go to config.php in cofig folder and set
hope this will help
thanks
检查您是否在配置文件
/application/config/config.php
中配置了某些内容,例如Check if you have something configured inside the config file
/application/config/config.php
e.g.我认为您尚未编辑 codeigniter 文件来启用 base_url()。您尝试在 url_helper.php 中分配它,您也可以执行相同的 config/autoload.php 文件。你可以在你的 autoload.php 中添加这段代码
,然后你就可以像这样使用 base_url()
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
Than You will be able to ue base_url() like this
除了确保您已设置 config/autoload.php:
将 application/config/config.php 更改为:
成为动态基本 url:
并使用 php 中的主机在本地运行它,下面只是一个示例端口。
Apart from making sure you have set config/autoload.php:
Change application/config/config.php from:
Become a dynamic base url:
And using the host from php to run it on local, below is just an example port.
如果您不想使用 url 帮助程序,则可以使用以下变量获得相同的结果:
它将为您返回基本 url,无需额外的步骤。
If you don't want to use the url helper, you can get the same results by using the following variable:
It will return the base url for you with no extra steps required.
在控制器中加载 url 助手
$this->load->helper('url');
Load url helper in controller
$this->load->helper('url');
如果你直接在 Codeigniter 框架中直接使用任何东西,比如
base_url()
、uri_string()
或word_limiter()
,所有这些都是来自框架的某种Helper
功能。虽然某些
Helpers
可以全局使用,就像log_message()
一样在任何地方都非常有用,但其余的 Helpers 是可选的,并且用例因应用程序而异。base_url()
是框架的url
帮助器中定义的函数。您可以在 Codeigniter 用户指南的帮助程序部分了解有关帮助程序的更多信息。
您可以使用
base_url()
一旦你当前的类可以访问它,你需要首先加载它。在使用
base_url()
函数之前,您可以在应用程序中的任何位置使用此行。如果您需要经常使用它,我建议在
autoload helpers
部分的config/autoload.php
中添加此函数。另外,请确保您在
config/config.php
文件中定义了明确的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()
, orword_limiter()
, All of these are coming from some sort ofHelper
function of framework.While some of
Helpers
may be available globally to use just likelog_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 inurl
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.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 theautoload helpers
section.Also, make sure you have well defined
base_url
value in yourconfig/config.php
file.This will be the first configuration you will see,
You can check quickly by
Reference: https://codeigniter.com/user_guide/helpers/url_helper.html
问题 - 我想加载我的 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
我花了几个小时遇到这个问题,但以不同的方式解决了它。您可以看到,我刚刚在应用程序文件夹之外创建了一个资产文件夹。最后,我在页眉部分链接了样式表。文件夹结构如下图所示。
在执行此操作之前,您应该在控制器类方法/__constructor 文件中或在 autoload.php 文件中包含 url 帮助程序文件。 还可以在以下文件 application/config/config.php 中更改
$config['base_url'] = 'http://yoursiteurl';
如果您将其包含在控制器类中, method/__constructor 那么它看起来像
或者如果您加载自动加载文件那么它看起来像
最后,添加您的样式表文件。
您可以通过不同的方式链接样式表,将其包含在内部部分
->
->或
->或者
最终我得到了更可靠的代码清理概念。只需在 application/config/styles.php 文件夹中创建一个名为 styles.php 的配置文件即可。
然后在 styles.php 文件中添加一些链接,如下所示
调用/将此配置添加到您的控制器类方法如下所示
在标头模板中传递此数据如下所示。
最后添加或链接您的 css 文件,如下所示。
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.
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.phpIf you include it in controller class method/__constructor then it look like
or If you load in autoload file then it would looks like
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
-> 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
call/add this config to your controller class method looks like below
Pass this data in your header template looks like below.
And finally add or link your css file looks like below.
所有直接访问的函数将仅通过帮助程序类加载。
像 URL、安全性、文件都是帮助器,您还可以加载自定义帮助器。
配置/自动加载.php
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