How to link a custom CSS file in CakePHP2

54 Views Asked by At

How can I link a custom CSS file to my view in CakePHP2? I have a page that needs a unique css style and would like to know how to connect a custom stylesheet.

I've looked for an answer to this question and couldn't find one. I figured out the answer by searching my codebase, I'm just putting this here to help others.

1

There are 1 best solutions below

0
Austin Poulson On BEST ANSWER

The best way I've found to do this is by creating an array of css file names in the controller, then using the form creator in the view.

This goes in your controller:

$this->set('cssIncludes' => array('CSSFileName'));

This goes in your view:

if (isset($cssIncludes)) echo $this->Html->css($cssIncludes);

The CSS file goes in webroot/css/CSSFileName.css

This format allows you to specify multiple css files with minimal effort. It's also fairly robust.