Including a javascript file outside of doc root using zend framework

馋奶兔 提交于 2019-12-06 08:36:17

Based on previous questions you've been asking I think your directories are something problematic for you. here is a functionnal and secure example or directory organization for Zend Framework (partial)

var/
   www/
     vhosts/
        otherproject/
        project/
             src/ <-- maybe some project src can be here and not in your libraries
             htdocs/ <--- real apache document root
                css/
                js/
             var/
               log/
               sessions/
             etc/
             doc/
        libraries/
             Zend/
             my-lib/
                  js/

So apache documentRoot is /var/www/project/htdocs. Here we can find the index.php file, the only php file available on public access.

In htdocs/js & htdocs/css you can put some of your project js & css files. And then you've got the problem of css and js files of your external php libs that are now completly outside of the web root.

What I usually do is, like others have said here, links from external directories to the js directory inside the web root. But to be more precise and keep things well organized here what you should do there:

 ln -s /var/www/project/libraries/my-lib/js /var/www/project/htdocs/js/my-lib
 ln -s /var/www/project/libraries/my-lib/css /var/www/project/htdocs/css/my-lib

And you should do it for all external lib having files that should be in the document root. This way the base url for the js files of my-lib is /js/my-lib/.

Do not fear of using symlinks (junctions on windows), you can even store them in subversion repository. Just check that your apache configuration allow symlinks (Options +FollowSymlinks)

The path provided in appendFile() is relative to the site's document root (eg, your 'public' folder). It will not pick up on the php include_path.

You could move the js file into the doc root, create a symbolic link to it in the doc root, or you could read the file using php and output it's contents as a <script> tag.

form your path , i can tell your are using linux so you can use symlink like this :

ln -s /var/www/vhosts/libraries/my-lib/ /var/www/vhosts/project/mylib/

therefor you can append the files :

<?php $this->headScript()->appendFile('/mylib/js/javascript.js'); ?> 

and tada , its done

Scoobler

The tag that will be added to the page is a reference for the browser where to look for the JavaScript file.

JavaScript is a client side language, it runs on the users computer and is interpreted there, so the user needs to be able to access the file, hence it needs to be inside the root path as the user (client) should not have access to your application dir.

You could save a PHP file in your doc root and use that to get your JS:

getJS.php (saved in the doc root):

<?php
    header("Content-type: text/javascript");
    include_once '/../var/www/vhosts/libraries/my-lib/js/someJSfile.js';  
?>

Then in your code:

<?php 
    $this->headScript()->appendFile('getJS.php'); 
?>

You could include switches to include different JS files or whatever you wanted, I haven't tested this for functionality, but the file when clicked does get the contents of the JS file.

Note: If this is for security reasons, it won't take much to get the contents of the file the user wants!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!