Html->script([ 'base.js', ], [ 'fullBase' => t" /> Html->script([ 'base.js', ], [ 'fullBase' => t" /> Html->script([ 'base.js', ], [ 'fullBase' => t"/>

Loading JS files in CakePHP 3

112 views Asked by At

I have a template inside a plugin in my project, that is trying to load a "general" JS file:

<?= $this->Html->script([
    'base.js',
], [
    'fullBase' => true,
]) ?>

public/ is the root folder of the whole project, and the bootstrap.ctp template file resides inside the plugin folder:

/var/www/repo/publi/plugins/MyPlugin/src/Template/Layout/bootstrap.ctp

while the base.js file resides in the "general" webroot folder:

/var/www/repo/public/webroot/js/base.js

When I visit the page, this is the error I get:

GET https://localhost/var/www/repo/public/js/base.js net::ERR_ABORTED 404 (Not Found)

This is the output of the $this->Html->script() call:

<script src="/var/www/repo/public/js/webroot/base.js"></script>

And this is the content of my .htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule    ^$    webroot/    [L]
    RewriteRule    (.*) webroot/$1    [L]
</IfModule>

But when I add

dd(WWW_ROOT);

at the beginning of my template file, I get the correct output with the path that I would expect:

/var/www/repo/public/webroot/

So, my question is: if the WWW_ROOT constant is correctly configured, why is the script() function loading the base.js file incorrectly?

CakePHP's own documentation even states that

This method of JavaScript file inclusion assumes that the JavaScript file specified resides inside the webroot/js directory:

which is exactly what I'm doing.

0

There are 0 answers