How to insert external CSS into Codeigniter 3

Asked

Viewed 2,140 times

4

I would like to know how to insert an external css file into a Codeigniter 3 view. I set up the $autoload['helper'] = array('url'); I tried to make the call in the following ways:

<link rel="stylesheet" type="text/css" href="<?=site_url('application/views/css/main.css')?>">
<link rel="stylesheet" type="text/css" href="<?=site_url('css/main.css')?>">

I also tested using the base_url(); but the results were the same. It prints the html with the correct path but is not applying the styles. If I click this link inside the browser’s source viewer it redirects me to the default 404 codeigniter.

  • How is your apache’s folder structure? localhost points to your project or needs to enter one more directory level?

  • Show how this folder structure of yours

  • The problem was in my htaccess. was talking include the directory in the rule. Before: Rewritecond $1 ! (index.php|/css|js|images|robots.txt) After:Rewritecond $1 ! (index.php|includes/css|includes/js|includes/images|robots.txt)

1 answer

4


EDIT

Be sure to call the help method in the Controller or the helper URL.

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

ORIGINAL

Use the base_url() same. But then you have to configure the URL to return the path of the root folder of your site.

Assuming a structure like this:

  • www.raiz.com
    • index php.
    • css
      • meucss.css

And if you want to include CSS in the index.php file, then base_url would be $config['base_url'] = "http://www.raiz.com/".

In index.php, you would call CSS:

<link href="<? echo base_url();?>/css/meucss.css" rel="stylesheet" type="text/css" />

  • When you say: "And you want to include CSS in the index.php file, then base_url would be $config['base_url'] " You mean in the view? or in the controller?

  • 1

    I refer to the Codeigniter configuration file on application/config/config.php

  • I configured it as follows: No config.php $config['base_url'] = 'localhost/Codeigniter-3.0.3/'; Na view: <link rel="stylesheet" type="text/css" href="<? php echo base_url();? >css/main.css"> The result in the generated html was: <link rel="stylesheet" type="text/css" href="localhost/Codeigniter-3.0.3/css/main.css"> But it still has the same problem of not inserting and if I try to click on the source view link in the browser, it takes me to the codeigniter error page.

  • @Jahnkrauss try to http://localhost/CodeIgniter-3.0.3/. Just to understand, why not use the same relative url?

  • even with the relative url it does not access!

  • You added the help method in Controller? $this->load->helper('url');

  • 1

    I discovered the problem, there was a change in codeigniter in patch 3.0.3, I will collect more information and post here for future questions. At first it should now be used in another way, it was done to avoid Host header injections.

  • By simplifying the CSS call according to the response, you can use <?= and ?> in place of <? echo and ;?> and use the directory within the function base_url(). Example: <link rel="stylesheet" type="text/css" href="<?=base_url('css/meucss.css')?>">

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.