M Saikat
M Saikat

Reputation: 11

Access base_url() Or site_url() in CodeIgniter's custom config file

I want to accessbase_url() method so that I can get my application url in custom config file.

I don't want to write $config['base_url'] = 'some url'; in custom config file.

Upvotes: 1

Views: 1424

Answers (3)

Tpojka
Tpojka

Reputation: 7111

You can use $this->config array that contains all settings loaded by default. For example: you can create sample.php config file and put next code into

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/*
| -------------------------------------------------------------------------
| Sample
| -------------------------------------------------------------------------
|
|
*/

//here you can see how is used base_url value from config.php
$config['r'] = $this->config['base_url'];

In controller you are calling it as any other config item:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller
{
    public function index()
    {
        var_dump($this->config->item('r'));
    }
}

Autoload sample.php in config section of autoload.php file or manage loading as appropriate for your application.

Upvotes: 0

user4419336
user4419336

Reputation:

Your question unclear but give it ago. To enable base_url load the url helper

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

$autoload['config'] = array('custom'); // application > config / custom.php

The create the custom config file

<?php

// Testing
echo config_item('base_url');

$config['test'] = '111';

Or on controller load in the __construct area

Check filenames and classes starts with first letter only upper case

<?php

class Welcome extends CI_Controller {

 public function __construct() {
    parent::__construct();
    $this->load->helper('url');
 }

}

Make sure you have set your base url in CodeIgniter 3 and above versions it is recommended.

$config['base_url'] = 'http://localhost/project/'; 

Upvotes: 1

Syden
Syden

Reputation: 8625

To use base_url(), you must first have the URL Helper loaded. This can be done in application/config/autoload.php, then:

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

Or manually:

$this->load->helper('url');

To print returned value:

echo base_url();

Upvotes: 0

Related Questions