Search code examples
phpcodeigniter-url

CodeIgniter: Add css link


I am trying to use the PHP Framework CodeIgniter for a PHP project. I never used it before. According to its documentation I activated the helper URL and included the css link as shown below:

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

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

Unfortunately the page is not able to load the css. Obviously if I put the compiled CSS link the page is able to load it.

<link rel="stylesheet" 
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">

Solution

  • Base_url() does not add a trailing slash. You need a forward slash in between the PHP code and public in the link href.

    Say your base url is /htdocs/website/, the way you currently have it set up would print out /htdocs/websitepublic/css/style.css.

    Here is the fixed code:

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