0

I want to add script files and css files to my php view page inside a module, I try to make like this

   <base href="<?php echo $this->baseUrl(); ?>" />
    <link rel="stylesheet" type="text/css"  href="<?php echo $this->baseUrl('style/ui-lightness/jquery-ui-1.8.18.custom.css'); ?>"/>

and like this

    <?php
    $this->headScript()->appendFile('js/jquery-1.7.1.min.js');
    echo $this->headScript();
    ?>

but both of them do not inlucde the files. I put my css and js files inside the helpers folder of view folder.

1 Answer 1

2

Your JS and CSS files are in the wrong place.

The helpers folder of the the view folder isn't even in the web root (by default), so you literally cannot request it from the web server.

Put them somewhere under the web root, and then give a path to them in the view script.

As your view script may be a few folders deep, I tend to use absolute paths. (/js/jquery...).

You could alternatively programmatically calculate the absolute or relative path to the file and use it. (If you're at controller1/ you'll need to do ../js/jquery, but if you're at controller1/action/ you'll need to do ../../js/jquery.)

Sign up to request clarification or add additional context in comments.

2 Comments

It's not accepted to put them on the root because I am using modules.
The typical setup is to have the public folder be the root of the domain (or subdomain) and then use absolute paths to JS/CSS/images so on. Anything else introduces the complications of calculating paths.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.