Bootstrap files not load properly in other codeign

2019-01-29 12:24发布

问题:

load view file from index function it there no problem to load bootstrap files.

public function index()
{
    $this->load->view('page_one');      
}

When I try to load the view from other functions, then the bootstrap files does not load properly.

public function fun_one()
{
    $this->load->view('page_one');      
} 

回答1:

It seems you are using relative URLs for your assets. I could suggest two ways to resolve this issue:

  • Use a HTML <base> element to specify the base URL for all relative URLs.
  • Use absolute URLs for the assets.

Using <base> element

You could use the HTML <base> element in the <head>, and set the href attribute by using CodeIgniter base_url() function:

<base href="<?php echo base_url(); ?>">

Then all the relative URLs use this href attribute as the base URL address.

The potential disadvantage of using <base> is that it affects the anchor tags as well.

Using absolute URLs

In this approach, you need to add the base_url() to each assets' URL address. For instance:

<link rel="stylesheet" href="<?php echo base_url(); ?>assets/css/bootstrap.css">
<script src="<?php echo base_url(); ?>assets/js/bootstrap.js"></script>