BAD_SEED
BAD_SEED

Reputation: 5056

CakePHP: Include PHP file with script function

I have a file called script.php, this is some php and some javascript mixed together. The mime type of this file is forced to javascript, so this is a normal javascript file, but extension php.

I'm tryng to insert this inside my layout throuh script function. If write:

echo $this->Html->script('rsravenna/script.php'); 

cakephp will try to insert script.php.js, how can avoid the append of .js?

Upvotes: 0

Views: 958

Answers (2)

bfncs
bfncs

Reputation: 10316

The appending of .js is hardcoded in the HtmlHelper->script() function. Is there anything in your app, that keeps you from just including your script manually?

<script type="text/javascript" src="myscript.php"></script>

Otherwise you could extend the HtmlHelper to pass the variable ext as part of the options array to HtmlHelper->assetUrl.

You could also do it the other way around, tell your server to parse .js-files in a specific folder as php to preserve the extension. If you're using apache, put this in your .htaccess:

AddType application/x-httpd-php .js

Now you should be able to rename your myscript.php to myscript.js and still get it parsed. This would enable you to use the standard script()-function.

Upvotes: 1

Walter Schweitzer
Walter Schweitzer

Reputation: 424

It's really not clear to me what you're trying to do. script is an HTML Helper used to include javascript not PHP.

You can do something like:

echo $this->Html->url('/rsravenna/script.php', true);

or

echo $this->Html->url(array(
    "controller" => "foo",
    "action" => "bar",
    "ext" => "php"
));

Why are you forcing the MIME type to .js ?

Upvotes: 0

Related Questions