1

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.

3
  • Try $this->config->item('base_url'). Commented Nov 29, 2016 at 0:40
  • This is not working Commented Nov 29, 2016 at 4:04
  • @MSaikat have added my answer Commented Nov 30, 2016 at 0:33

3 Answers 3

1

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/'; 
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Loading url helper at autoload.php first. But while going to use base_url() at custom config file (config/my_config.php) throwing error i.e. undefined method base_url()
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.