Codeigniter + CSS

How to load css in codeigniter

The function base_url() should return the base path (without index.php)

You may fix it by adding a backslash like:

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

or remove the index.php from your config:

$config['base_url'] = 'http://webscarlets.com/ci/';

How can I load css or js file in codeigniter 4?

As app is sibling folder of public folder,

And as per codeIgniter 4 .

anything you put in public folder can be accessed directly.

so it should be something like below to get css applied to the view.

In views folder lets say there's a file as index.php

then code should be like below:

index.php

    <link rel="shortcut icon" type="image/png" href="/css/style.css"/>

CodeIgniter - css file doesn't work

Put your asset folder in project root, out side application folder.

Change the link to,

<?php echo link_tag('assets/css/style.css')?>

CI Folder Structure

Sample Image

How can I include css and js file in codeigniter

As far as i can see, you din't declared $config['css'] in your config file, so it's normal to get undefined variable error for css. But you shouldn't have problem with js.

When declaring your base_url use trailing slash at the end (eg. "http://localhost/fancysite/")

Also you can use CI's url helper to use functions like base_url() or site_url() and many more. (as @Likee suggested).

config.php

// this should be the first variable in CI's config.php
$config['base_url'] = "http://localhost/ASOFT/Projects/CI_search/";

// many more lines with other configuration variables
// ..................................................
// ..................................................
// ..................................................

// your own configuration variables at the end of file
$config['css'] = 'css/';
$config['js'] = 'assets/css/';
$config['image'] = 'images/';

controller

public function index() {

// loading url helper to use base_url() function in the view,
// if you load this helper in autoload.php you don't need to load it here again
$this->load->helper('url');

// if you didn't declare data as class property
// you can simply use
// $data = array(
// 'css' => $this->config->item('css'),
// 'js' => $this->config->item('js'),
// 'image'=>$this->config->item('image')
// );

$this->data = array(
'css' => $this->config->item('css'),
'js' => $this->config->item('js'),
'image'=>$this->config->item('image')
);

// you really don't need the line below
// $data = $this->data;
$this->data['error'] = '';

$this->load->view('index',$this->data);
}

view

<link rel="stylesheet" href="<?php echo base_url($css . 'style.css'); ?>">
<link rel="stylesheet" href="<?php echo base_url($css . 'bootstrap.min.css'); ?>">
<script src="<?php echo base_url($js . 'jquery.min.js'); ?>"></script>
<!-- do you really need to include jquery.js while you included jquery.min.js above? but here we go :) -->
<script src="<?php echo base_url($js . 'jquery.js'); ?>"></script>
<script src="<?php echo base_url($js . 'bootstrap.min.js'); ?>"></script>

Probably you will need to use url helper a lot. So you can autoload it in autoload.php file in config folder. it must be somewhere around line 90

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

CodeIgniter not loading CSS and JS while migrating website from one domain to another domain and hosting

Your base_url is defined as

$config['base_url'] = 'http://localhost/domain.com';

You will need to load the url helper in your controller or you can autoload it, then you can utilise the function base_url().

Then you have a choice of either using

<link rel="stylesheet" type="text/css" href="<?= base_url('asset/css/bootstrap.min.css');?>">

OR you can assign FRONT_MEDIA_URL

defined('FRONT_MEDIA_URL') || define('FRONT_MEDIA_URL', base_url());

BUT you will need to have loaded the url helper prior and you've not shown where you have declared that, but that is for you to figure out.

UPDATE due to new information
See if you can use this
Now I do not know where you are defining sSITE_MODE but this is an option to help with the use of base_url() in your site...

In application/config/config.php you could do something like

defined('sSITE_MODE ') || define('sSITE_MODE', 'live');

if (sSITE_MODE == 'live') {
$migration_url = 'https://www.theother2thirds.net/';
} else if (sSITE_MODE == 'beta') {
$migration_url = 'https://www.theother2thirds.net/';
} else {
$migration_url = 'http://localhost/theother2thirds/';
}

$config['base_url'] = $migration_url;

OR just define the correct Constant and use it. You have FRONT_MEDIA_URL in your links and then you are talking about using URL. You need to use the correct one.

CSS File is not loading in my CodeIgniter PHP

Open your browser console by right clicking on your web page and select inspect.

Go to your network requests / console and check what URL you get for your linked files (CSS).

If you find out that the path you have given in your application if different then you need to change your code accordingly to get the appropriate path.



Related Topics



Leave a reply



Submit