php 如何在 CodeIgniter 2.* 中获取基本 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7503302/
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
How to get base url in CodeIgniter 2.*
提问by Hai Truong IT
In config.php
在 config.php
$config['base_url'] = 'http://localhost/codeigniter/';
In View
在视图中
<link rel="stylesheet" href="<?php base_url(); ?>css/default.css" type="text/css" />
=> Error: Call to undefined function base_url();
Help me
=> Error: Call to undefined function base_url();
帮我
回答by Muhammad Usman
To use base_url()
(shorthand), you have to load the URL Helper
first
要使用base_url()
(速记),您必须加载第URL Helper
一个
$this->load->helper('url');
Oryou can autoload it by changing application/config/autoload.php
或者您可以通过更改自动加载它application/config/autoload.php
Orjust use
或者只是使用
$this->config->base_url();
Same applies to site_url()
.
同样适用于site_url()
.
Also I can see you are missing echo
(though its not your current problem), use the code below to solve the problem
我也可以看到你丢失了echo
(虽然它不是你当前的问题),使用下面的代码来解决问题
<link rel="stylesheet" href="<?php echo base_url(); ?>css/default.css" type="text/css" />
回答by KutePHP
I know this is very late, but is useful for newbies. We can atuload url helper and it will be available throughout the application. For this in application\config\autoload.php modify as follows -
我知道这很晚了,但对新手很有用。我们可以 atuload url helper,它将在整个应用程序中可用。为此在 application\config\autoload.php 中修改如下 -
$autoload['helper'] = array('url');
回答by birderic
You need to load the URL Helper in order to use base_url()
. In your controller, do:
您需要加载 URL Helper 才能使用base_url()
. 在您的控制器中,执行以下操作:
$this->load->helper('url');
Then in your view you can do:
然后在您看来,您可以执行以下操作:
echo base_url();
回答by birderic
Just load helper class
只需加载助手类
$this->load->helper('url');
thats it.
就是这样。
回答by birderic
You need to add url helper in config/autoload
您需要在 config/autoload 中添加 url helper
$autoload['helper'] = array('form', 'url', 'file', 'html'); <-- Like This
Then you can use base_url or any kind of url.
然后你可以使用 base_url 或任何类型的 url。