简体   繁体   English

在CodeIgniter上添加URL变量

[英]Adding URL Variable on CodeIgniter

I want to add a new URL variable on CI 3.0 like site_url or base_url . 我想在CI 3.0上添加一个新的URL变量,例如site_urlbase_url

For example; 例如; I want to add an admin_url variable for administration area and assets_url variable for assets. 我想补充的admin_url用于管理区和可变assets_url资产变量。

I checked CI 3.0 guide but couldn't find a solution. 我检查了CI 3.0指南,但找不到解决方案。

Thanks in advance. 提前致谢。

It's pretty straight forward. 非常简单。

Just go to your config.php which is located at /your_root/application/config directory 只需转到位于/ your_root / application / config目录中的config.php

add at this line at the bottom of that file 在该文件底部的这一行添加

$config["admin_url"] = "http://www.your_url.com/admin";
$config["assets_url"] = "http://www.your_url.com/assets";

To retrieve it anywhere in application use this 要在应用程序中的任何位置检索它,请使用此

$your_admin_variable =$this->config->item("admin_url");
$your_assets_variable =$this->config->item("assets_url");

Your're in business :) 你在做生意:)

My solution. 我的解决方案。

Create new helper file and add this file to autoload. 创建新的帮助程序文件,并将此文件添加到自动加载。

So.. 所以..

Create file application/helpers/global_helper.php 创建文件application/helpers/global_helper.php

Inside your helper create functions for example: 在您的助手内部创建函数,例如:

<?php
    function admin_url(){
        return base_url() . "/admin/";
    }

Now edit config/autoload.php 现在编辑config/autoload.php

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

Add to exists array your new helper. 添加到现有数组中新的助手。

And now anywhere you can use your function admin_url 现在,您可以在任何地方使用函数admin_url

in system/helpers/url_helper.php you will find 在system / helpers / url_helper.php中,您会找到

if ( ! function_exists('base_url'))
{
    function base_url($uri = '')
    {
        $CI =& get_instance();
        return $CI->config->base_url($uri);
    }
}

so i guess if you create your own code like this 所以我想如果你像这样创建自己的代码

if ( ! function_exists('your_variable_name'))
{
    function your_variable_name($uri = '')
    {
        $CI =& get_instance();
        return $CI->config->your_variable_name($uri);
    }
}

but it is better to extend the helper rather modifying it , so you can use the code above in application/helpers/MY_url_helper.php and then you can call your custom variable as you normally do it with base_url hope that helps 但是最好扩展辅助程序而不是对其进行修改,因此您可以在application/helpers/MY_url_helper.php使用上面的代码,然后可以像通常那样使用base_url调用自定义变量,希望对您有所帮助

I got the answer now, 我现在得到了答案,

add these lines on system/helpers/url_helper.php file: system/helpers/url_helper.php文件中添加以下行:

if ( ! function_exists('admin_css'))
{
    /**
     * Base URL
     *
     * Create a local URL based on your basepath.
     * Segments can be passed in as a string or an array, same as site_url
     * or a URL to a file can be passed in, e.g. to an image file.
     *
     * @param   string  $uri
     * @param   string  $protocol
     * @return  string
     */
    function admin_css($uri = '', $protocol = NULL)
    {
        return get_instance()->config->admin_css($uri, $protocol);
    }
}

and add these lines on system/core/Config.php 并将这些行添加到system/core/Config.php

public function admin_css($uri = '', $protocol = NULL)
    {
        $base_url = base_url('assets/staff/css').'/';

        if (isset($protocol))
        {
            $base_url = $protocol.substr($base_url, strpos($base_url, '://'));
        }

        if (empty($uri))
        {
            return $base_url.$this->item('index_page');
        }

        $uri = $this->_uri_string($uri);

        if ($this->item('enable_query_strings') === FALSE)
        {
            $suffix = isset($this->config['url_suffix']) ? $this->config['url_suffix'] : '';

            if ($suffix !== '')
            {
                if (($offset = strpos($uri, '?')) !== FALSE)
                {
                    $uri = substr($uri, 0, $offset).$suffix.substr($uri, $offset);
                }
                else
                {
                    $uri .= $suffix;
                }
            }

            return $base_url.$this->slash_item('index_page').$uri;
        }
        elseif (strpos($uri, '?') === FALSE)
        {
            $uri = '?'.$uri;
        }

        return $base_url.$this->item('index_page').$uri;
    }

don't forget to autoload url_helper . 不要忘记自动加载url_helper With this way, you can use admin_css variable like this: <?php echo admin_css('foo.css'); ?> 通过这种方式,您可以像这样使用admin_css变量: <?php echo admin_css('foo.css'); ?> <?php echo admin_css('foo.css'); ?>

If you use the other answers on this post, you can not use like <?php echo admin_css('foo.css'); ?> 如果您在这篇文章中使用其他答案,则不能使用<?php echo admin_css('foo.css'); ?> <?php echo admin_css('foo.css'); ?>

Thank you all. 谢谢你们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM