0

PHP files can be used as external javascript files. Basically make a php file output valid javascript and use that php file as your javascript file: http://www.javascriptkit.com/javatutors/externalphp.shtml . Can this be done with cakephp since we don't specify php files in the browser but rather a directory based on controllers and their actions?

3 Answers 3

1

Late answer, but anyway, this is how I did it.
When linking to external javascript file, don't forget to set inline to false like the one shown below:

$this->Html->script('scriptname', array('inline' => false));
Sign up to request clarification or add additional context in comments.

Comments

0

Sure, as long as you output valid JS, id does not matter what the URL looks like and what is behind that URL.

Comments

0

When you link a javascript file with

$this->Html->script('scriptname');

all that happens is that a tag is created in the HTML

<script type="text/javascript" src="path/to/webroot/js/scriptname.js"></script>

So, you can link whatever you'd like.

6 Comments

sorry, i should have been more clear and through about my question. I want a view that only has javascript in it, then in a totally different application somewhere else, on a different server to access the that view file as if it was javascript file. did i make any sense?
@amirrustam If I understand you correctly, what you need is to create a layout that has nothing in it. This way, the javascript won't be surrounded by your usual HTML template. You can set the layout in the controller's action method.
and then how would i reference (javascriptkit.com/javatutors/externalphp.shtml) that view (the one with the empty layout) as a javascript file in a different application (as in NOT an php application) on different server somewhere else.
@amirrustam You would just use the URL in the same way that you would use any other CakePHP url: yourdomain.com/controller/action. The only thing to make sure of is that you set the Content-type to 'text/plain' in PHP.
In cakephp if we have a controller named "foos" and in that controller we have an action named "bar" then i can go that by site.com/foos/bar. Now I will set the layout for the "bar" action to be empty (just like you said) and then output just javascript. so essentially the view for the "bar" action is a javascript file. So i want to use that outputted javascript code as javascript file and reference it from another non-php application somewhere else.
|

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.