Why is the JavaScript file parsed as PHP in my Cake plugin?
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 relate开发者_如何学JAVAd 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:
- Create a symbolic link from the plugin webroot to the app webroot (e.g.
app/webroot/tinymce => app/Plugin/TinyMCE/webroot
) - Turn off PHP short tags in your PHP INI - a good principle anyway
In short, CakePHP may not fix this, as they want to maintain support for being able to include PHP code in JS/CSS files.
You need to put the js file into APP/plugins/your_plugin/webroot/js/ and access them like /your_plugin/js/file.js. If you dont do that the dispatcher wont be able to pass you the right file and try to load a controller instead.
精彩评论