I am developing an custom API for a web solution and I am using the MVC design pattern. I have a modules folder so that I can swap in and out modules and also work on sections without disrupting working tested code. My only issue now is that I want to load CSS anywhere and have my application properly import the css file in the head tag. I know CodeIgniter does this but I'm not sure how.
Using PHP, how do I load in a CSS file anywhere and then have the code properly import the css within the head tags like CodeIgniter does?
Thanks in advance.
Adding JavaScript and CSS (Cascading Style Sheet) file in CodeIgniter is very simple. You have to create JS and CSS folder in root directory and copy all the . js files in JS folder and . css files in CSS folder as shown in the figure.
Auto-loading Models If you find that you need a particular model globally throughout your application, you can tell CodeIgniter to auto-load it during system initialization. This is done by opening the application/config/autoload. php file and adding the model to the autoload array.
APPPATH is the path to your application folder. If you want to create an assets folder in your CodeIgniter root directory, create an assets folder in root, open your assets folder, create a CSS file named style. css, put it into the CSS folder, and create your js file called script. js and keep it into js folder.
Then in the middle of the file edit the two variables $application_folder and $system_path and make sure you're using an absolute path instead of a relative one. Then in your external PHP script just include the external. php file and use the $CI global object to access the whole codeigniter: include '../../external.
You can load several views at once, or views inside other views.
So in this case I recomend you to create one header view where you load all css and js files
example:
<html> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <link rel="stylesheet" href="<?php echo base_url();?>css/moorainbow.css" type="text/css" media="screen"/> </head> <body>
And call it like:
$this->load->view('header'); $this->load->view('view1'); $this->load->view('view2');
This way you can control the files (css+js+etc) you load in just one file.
Regrads,
Pedro
@pcamacho
Your question is a little unclear to me, but I'll do my best to help. Are you simply wondering how to include a CSS file in your Views? If so, simply use the following:
<style> @import url('/css/styles.css'); </style>
If your CSS folder is at the root of your CodeIgniter project, you could do something like this using CodeIgniter's base_url() function:
<style> @import url('<?=base_url()?>/css/styles.css'); </style>
It will ensure your pages stay portable and have the correct absolute URL. Hope this helps! If not, try being a little more specific in your question
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With