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
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
Amir Rustamzadeh
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?
mdarwi
@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.
Amir Rustamzadeh
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.
mdarwi
@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.
Amir Rustamzadeh
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.
|