I’ve got a problem with one of my JavaScripts included in a Cake plugin I’m developing. For some reason a JavaScript file is parsed as a PHP file, so loading the file throws this error, which is a PHP parsing error:
<b>Parse error</b>: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING
The script is called using the HtmlHelper:
<?php echo $this->Html->script(array(
'/myplugin/scripts/tiny_mce/tiny_mce',
)); ?>
The error is triggered by a compressed tiny_mce.js file which resides in a subdirectory in a scripts dir, the uncompressed version displays the same behaviour. The thing is that I haven’t got the problem on my local setup.
The remote server is running PHP 5.2.17 with mod_rewrite enabled, my Cake version is 1.3.10 – my test setup runs PHP 5.3.4 and the same version of Cake.
It could be a rewrite or permissions error, but my application functions fine otherwise and other scripts (including those in subdirectories and remote ones) load without problem.
EDIT:
After moving the tiny_mce directory out of the Cake installation and linking to it the JavaScript is parsed correctly. My guess it’s a bug relating to mod_rewrite and the handling of plugins, but I haven’t found other cases related to this behaviour.
I have also come across this issue, with exactly the same file: Tiny MCE JavaScript.
This is due to the way that the CakePHP dispatcher handles files in a plugin’s webroot. It chooses to do a PHP include on files with CSS or JS extensions, and it just so happens that the tiny_mce.js file contains the characters
<?, meaning that instead of just outputting JavaScript text it tries to parse the subsequent lines as PHP code. Unsurprisingly it fails!This has been raised as a bug on Lighthouse (CakePHP’s bug tracking system). The suggested fixes are:
app/webroot/tinymce => app/Plugin/TinyMCE/webroot)In short, CakePHP may not fix this, as they want to maintain support for being able to include PHP code in JS/CSS files.