CodeIgniter base_url

redirect base_url codeigniter
base url in codeigniter for localhost
codeigniter base_url link
call to undefined function base_url() codeigniter
codeigniter redirect
set dynamic base url in codeigniter
codeigniter href base_url
codeigniter base_url https

I have a problem with codeigniter base_url. My base_url is $config['base_url'] = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME'])... (http://test.localhost/)... but when I do an ajax request for example I use <?= base_url(); ?>installer/test and the response is "No direct script access allowed".

I try adding to base_url index.php (http://test.localhost/index.php/) and this problem is solved but now when I load an css using base_url like this: <?= base_url(); ?>assets/css/installer.css the css is not loading but if I remove index.php the css load correctly but the ajax request response "No direct script access allowed".

Obviously I think that I have a problem with my configuration but I don't find the error. I need that the ajax request and css load under the same base_url because the code is big and I can't change all instance of base_url.

base_url and site_url should be like this:

base_url() = http://test.localhost/
site_url() = http://test.localhost/index.php/

If you need to load URL resources like CSS, JS, images etc., use base_url(), otherwise site_url() is better. That means in your ajax request you can set the url like this:

var serviceUrl = "<?php echo site_url(); ?>your_controller/your_method";
            var returnResult = $.ajax({
                                            url: serviceUrl,
                                            data: {'id': id, .....},
                                            type: 'post',
                                            dataType: 'json'
                                        });

Or you can set the url like this:

var serviceUrl = "<?php echo base_url(); ?>index.php/your_controller/your_method";
            var returnResult = $.ajax({
                                            url: serviceUrl,
                                            data: {'id': id, .....},
                                            type: 'post',
                                            dataType: 'json'
                                        });

base_url() function not working in codeigniter, In order to use base_url() , you must first have the URL Helper loaded. This can be done either in application/config/autoload.php (on or around� Once it's loaded, be sure to keep in mind that base_url() doesn't implicitly print or echo out anything, rather it returns the value to be printed: echo base_url(); Remember also that the value returned is the site's base url as provided in the config file. CodeIgniter will accomodate an empty value in the config file as well:

Problem May be in...

  1. $_SERVER['HTTP_HOST'] is rarely works in localhost, sometimes it failed to detect host route.

  2. CSS stylesheet need the pure directory URL, It never include any other files with URL like 'localhost/index.php'

Try Using...

  1. $config['base_url'] ='http://test.localhost/';
  2. import css as => href="<?php echo base_url(); ?>assets/css/installer.css"
  3. Use Ajax as => 'url' : '<?php echo base_url(); ?>index.php/your controller path'

codeigniter, You will need to set your base URL in application/config/config.php. If it is not set, then CodeIgniter will try to guess the protocol and path to your installation, but� This helper is automatically loaded by the framework on every request. Returns your site URL, as specified in your config file. The index.php file (or whatever you have set as your site indexPage in your config file) will be added to the URL, as will any URI segments you pass to the function. You

I encountered with this issue spending couple of hours, however solved it in different ways. You can see, I have just created an assets folder outside application folder. Finally I linked my style sheet in the page header section. Folder structure are below images.

Before action this you should include url helper file either in your controller class method/__constructor files or by in autoload.php file. Also change $config['base_url'] = 'http://yoursiteurl'; in the following file application/config/config.php

If you include it in controller class method/__constructor then it look like

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

or If you load in autoload file then it would looks like

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

Finally, add your stylesheet file. You can link a style sheet by different ways, include it in your inside section

-><link rel="stylesheet" href="<?php echo base_url();?>assets/css/style.css" type="text/css" />

-> or

<?php

    $main = array(
    'href'       => 'assets/css/style.css',
    'rel'        => 'stylesheet',
    'type'       => 'text/css',
    'title'      => 'main stylesheet',
    'media'      => 'all',
    'index_page' => true
    );


echo link_tag($main); ?>

-> or

finally I get more reliable code cleaner concept. Just create a config file, named styles.php in you application/config/styles.php folder. Then add some links in styles.php file looks like below

<?php
 $config['style'] = array(
    'main' => array(
        'href'       => 'assets/css/style.css',
        'rel'        => 'stylesheet',
        'type'       => 'text/css',
        'title'      => 'main stylesheet',
        'media'      => 'all',
        'index_page' => true
    )
 );

?>

call/add this config to your controller class method looks like below

$this->config->load('styles');
$data['style'] = $this->config->config['style'];

Pass this data in your header template looks like below.

$this->load->view('templates/header', $data);

And finally add or link your css file looks like below.

<?php echo link_tag($style['main']); ?>

CodeIgniter - Configuration, Configuring Base URL. The base URL of the site can be configured in application /config/config.php file. It is URL to your CodeIgniter root. Typically� Base url in Codeigniter; Setting your base url in Codeigniter; Smart way to setting up the base_url; Something More About base_url; Calling a model method in a view; CAPTCHA Helper; CodeIgniter - Internationalization; Codeigniter Pagination; CodeIgniter Shopping Cart; Codeigniter Troubleshooting; CodeIgniter URI Segment

base_url is used to access image JS, files, and CSS and site URL is for accessing controller's function. In base_url index.php is not present but in site_url index.php press end to define base_url like this:

$config['base_url'] =  http://project_name/assets/file_name;
$config['site_url'] = http://project_name/controller/function;

codeigniter, You will need to set your base URL in application/config/config.php. If it is not set, then CodeIgniter will try to guess the protocol and path to your installation, but� Understanding base_url() First, let us understand what are base_url() as well as site_url(). base_url() is the URL of your CodeIgniter root or the location of the index.php file along with a trailing slash appended to the end of the URL. If the domain name is example.com and the website is using HTTPS as well as www.

Base url in Codeigniter, You will need to set your base URL in application/config/config.php. If it is not set, then CodeIgniter will try to guess the protocol and path to your installation, but� CodeIgniter's URL helpers are groups of utility functions which will help you to call ,create, extend and maintain url. In this tutorial we will give you brief description about how to use “URL Helper” in CodeIgniter which will help you in making ,calling and creating url.

codeigniter base_url Code Example, Get code examples like "codeigniter base_url" instantly right from your google search results with the Grepper Chrome Extension. In this CodeIgniter 4 crud application example tutorial, we will use bootstrap 4 for creating users list and create, edit form in CodeIgniter 4 projects. CodeIgniter 4 CRUD Application with Bootstrap. Follow the below steps and easily create a first crud application using CodeIgniter 4 framework with bootstrap 4: Download Codeigniter Latest

Using base_url in a view | General Discussion, Have a serach in the CodeIgniter User Guide and all will be revealed. Was it really that hard to just tell him that base_url isn't a variable? CodeIgniter 3 has a 2MB download, including the user guide. CodeIgniter 4 is a 1.2MB download, plus 6MB for the user guide. Much of the CodeIgniter configuration is done by convention, for instance putting models in a "models" folder. There are still a number of configuration options available

Comments
  • defined('BASEPATH') OR exit('No direct script access allowed'); is used to make sure that the request has gone through index.php in your root dir. This is for reasons such as making sure that all CI base classes are being loaded and making sure certain vars have been set etc.
  • @AbdulSalam please stop code-tagging the word "URL". It is not code and should not be tagged as such.