簡體   English   中英

在CodeIgniter上添加URL變量

[英]Adding URL Variable on CodeIgniter

我想在CI 3.0上添加一個新的URL變量,例如site_urlbase_url

例如; 我想補充的admin_url用於管理區和可變assets_url資產變量。

我檢查了CI 3.0指南,但找不到解決方案。

提前致謝。

非常簡單。

只需轉到位於/ your_root / application / config目錄中的config.php

在該文件底部的這一行添加

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

要在應用程序中的任何位置檢索它,請使用此

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

你在做生意:)

我的解決方案。

創建新的幫助程序文件,並將此文件添加到自動加載。

所以..

創建文件application/helpers/global_helper.php

在您的助手內部創建函數,例如:

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

現在編輯config/autoload.php

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

添加到現有數組中新的助手。

現在,您可以在任何地方使用函數admin_url

在system / helpers / url_helper.php中,您會找到

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

所以我想如果你像這樣創建自己的代碼

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

但是最好擴展輔助程序而不是對其進行修改,因此您可以在application/helpers/MY_url_helper.php使用上面的代碼,然后可以像通常那樣使用base_url調用自定義變量,希望對您有所幫助

我現在得到了答案,

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

並將這些行添加到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;
    }

不要忘記自動加載url_helper 通過這種方式,您可以像這樣使用admin_css變量: <?php echo admin_css('foo.css'); ?> <?php echo admin_css('foo.css'); ?>

如果您在這篇文章中使用其他答案,則不能使用<?php echo admin_css('foo.css'); ?> <?php echo admin_css('foo.css'); ?>

謝謝你們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM