简体   繁体   English

从codeigniter的主域和子域获取base_url

[英]Get base_url from main domain and sub domain in codeigniter

I have two application for my project 我有两个项目申请

  1. site.com site.com
  2. api.site.com api.site.com

And here's my config 这是我的配置

$config['base_url'] = 'http://' . $_SERVER['HTTP_HOST'] . '/';

If I run site.com , how to get base_url or site_url of api.site.com ? 如果我运行site.com ,如何获取api.site.com的base_url或site_url

If I run api.site.com , how to get base_url or site_url of site.com ? 如果我运行api.site.com ,如何获取site.com的 base_url或site_url?

If you use following code for base_url then you don't need to change anything you can use this in any server as well as any domain subdomain IP address 如果对base_url使用以下代码,则无需更改任何内容,即可在任何服务器以及任何域子域IP地址中使用此代码

$config['base_url'] = (isset($_SERVER['HTTPS']) ? "https://" : "http://") . 
  $_SERVER['HTTP_HOST'] . preg_replace('@/+$@', '', dirname($_SERVER['SCRIPT_NAME'])) . '/';

I hope site.com and api.site.com will have different content. 我希望site.com和api.site.com会有不同的内容。 It is best to have two application folders and two different index.php file in each root directory. 最好在每个根目录中有两个应用程序文件夹和两个不同的index.php文件。 You can share the same system files, by updating the system path in the index.php file. 您可以通过更新index.php文件中的系统路径来共享相同的系统文件。

There is no such function to retrieve the URL in your way. 没有这样的功能以您的方式检索URL。 You need to do it manually. 您需要手动进行。 Create a helper called base_helper.php inside helpers directory and write this code. helpers目录中创建一个名为base_helper.php helpers并编写此代码。

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

if (!function_exists('url')) {

    function url($param = TRUE)
    {
        if ($param) {
            return 'http://site.com/';
        }
        return 'http://api.site.com/';
    }

}

You can call this helpers in controllers/views you wish. 您可以在所需的控制器/视图中将此助手称为“助手”。 Use true and false parameter to get main domain or sub domain. 使用truefalse参数获取主域或子域。

$this->load->helper('base');
echo url();
echo "\n";
echo url(false);

Prints 版画

http://site.com/
http://api.site.com/

Hope this helps you. 希望这对您有所帮助。 Thanks!! 谢谢!!

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

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