Php 向Codeigniter配置中添加自定义函数,如site_url()&;基本url()
我们需要有一个新函数,比如base_url(),名为main_site_url(),以便能够像site_url()一样使用它 我刚刚将其添加到application/config中的主配置文件中:Php 向Codeigniter配置中添加自定义函数,如site_url()&;基本url(),php,codeigniter,Php,Codeigniter,我们需要有一个新函数,比如base_url(),名为main_site_url(),以便能够像site_url()一样使用它 我刚刚将其添加到application/config中的主配置文件中: $config['main_site_url'] = 'http//iksna.com/'; 并将此代码发送到/system/core/config.php /** * Main Site URL * Returns main_site_url . index_page [. uri_strin
$config['main_site_url'] = 'http//iksna.com/';
并将此代码发送到/system/core/config.php
/**
* Main Site URL
* Returns main_site_url . index_page [. uri_string]
*
* @access public
* @param string the URI string
* @return string
*/
function main_site_url($uri = '')
{
if ($uri == '')
{
return $this->slash_item('main_site_url').$this->item('index_page');
}
if ($this->item('enable_query_strings') == FALSE)
{
$suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
return $this->slash_item('main_site_url').$this->slash_item('index_page').$this->_uri_string($uri).$suffix;
}
else
{
return $this->slash_item('main_site_url').$this->item('index_page').'?'.$this->_uri_string($uri);
}
}
// -------------------------------------------------------------
但现在,它无法通过:main_site_url()访问代码>
是否可以通过以下方式访问:$this->config->main_site_url()
我在使用main_site_url()时出现此错误
错误:
Call to undefined function main_site_url()
去
创建一个名为
custom_function.php
在custom_function.php中添加函数main_site_url
使用调用控制器文件中的函数
$this->custom_function->main_site_url();
您可以通过以下方式创建自己的:
第1步:
在您的application/config/config.php
中添加这个,$config['main_site\u url']='http//iksna.com/'代码>
第二步:
在system/core/Config.php
中添加一个新函数main\u site\u url()
如base\u url()
,site\u url()
,该函数已在此处定义:
public function main_site_url($uri = '', $protocol = NULL)
{
$main_site_url = $this->slash_item('main_site_url');
if (isset($protocol))
{
if ($protocol === '')
{
$main_site_url = substr($main_site_url, strpos($main_site_url, '//'));
}
else
{
$main_site_url = $protocol.substr($main_site_url, strpos($main_site_url, '://'));
}
}
return $main_site_url.ltrim($this->_uri_string($uri), '/');
}
步骤3:
现在在system/helpers/url\u helper.php
if ( ! function_exists('main_site_url'))
{
function main_site_url($uri = '', $protocol = NULL)
{
return get_instance()->config->main_site_url($uri, $protocol);
}
}
现在,您可以在控制器、库和视图中的任何位置使用main\u site\u url()
,site\u url()
等。最好将常量文件写入其中,最好使用自定义变量$config['main\u site\u url']='your url';然后像$this->config->item('main_site_url')那样访问它;在站点中的任意位置。使用返回的函数site\u url()
创建自定义帮助程序,并自动加载。因为这只是我同意@Tpojka的一个函数-将其作为帮助程序。
if ( ! function_exists('main_site_url'))
{
function main_site_url($uri = '', $protocol = NULL)
{
return get_instance()->config->main_site_url($uri, $protocol);
}
}