Doug Wolfgram
Doug Wolfgram

Reputation: 2126

How do I make css load from a database with codeigniter

I am developing a CMS so I need each respective site's global css file to be stored in a database and loaded. I have a controller called util and the method is called sitecss. So my main wrapper view looks like this:

<link rel="stylesheet" type="text/css" href="/util/sitecss">

The css loads, but has no effect. If I view source on the page, and then click on the link, I can see the css just fine. So I know it is being loaded. Is it something about it not coming from a file? Or perhaps the browser assuming it is cached when it is not?

If I make a static file and change to above to

<link rel="stylesheet" type="text/css" href="/css/site.css">

everything works just fine. If I do this in .NET with a handler ashx file, it works fine. It is in php with Codeigniter that I am having the problem. I know someone will ask "why don't you just make static files?" and the answer is, it is not practical for this application. This is for thousands of sites with very rapid deployment.

Thanks in advance!

EDIT:

My controller method looks like this:

function sitecss() {
    $cssdata = $this->cmsutils->loadCss($this->session->userdata('sitecss'));
    echo $cssdata;
}

So can I just echo a mime-type first? It doesn't seem like this will work as I am making this call within the

Upvotes: 1

Views: 585

Answers (2)

NDBoost
NDBoost

Reputation: 10634

write a caching library to pull from the database and create a css file in a cache folder.

You will need:

Library Class

  • Interact with the css and create a form to perform CRUD
  • handle cache file monitoring CRUD (every hour or, even on every C,U,D of the form)
  • Inject the stylesheet cache file into the DOM view

Model

  • Interact with the database and perform CRUD operations
  • return data to the Controller for creating the cache file

View

  • parse out the values into a css stylesheet file format

The other option is to define a mime type with a controller and just load a view with the stylesheet properly formatted. No writing to the filesystem or anything.. Add a .css extension to the end of the URI and call it good...


I do this exact same thing for an app that I just released. I have a form in a view on the admin section of the app that has specified textfields. The user inputs hexadecimal color codes and then it saves/updates the data in the database. The library then creates a cached css file that is referenced in the header view. We did this to eliminate the need for us to add a .gitignore file in a special directory when we deploy the app to several clients.

Upvotes: 1

Rooster
Rooster

Reputation: 10077

Could you just load the css by passing it to the view from the controller and then echo it in the header somewhere or somewhere else in the view like this:

controller:

$data['css_rules'] = $this->yourcssmodel->get_css_rules_function();
$this->load->view('yadayadayada, $data);

view: (likely in header)

?>
<style>
<?echo $css_rules;?>
</style>

Upvotes: 1

Related Questions