Including a javascript file outside of doc root us

2019-05-23 16:08发布

I'm trying to include a javascript file in a phmtl view script file using the zend framework. Both the javascript file and the phtml file are part of a php library and located outside the doc root folder of my project. So the file structure looks like

/var/www/vhosts/project/
/var/www/vhosts/libraries/my-lib/view/viewscript.phtml
/var/www/vhosts/libraries/my-lib/js/javascript.js

/var/www/vhosts/libraries/my-lib has been added to the PHP paths using set_include_path. In viewscript.phtml, I use the following line to include javascript.js.

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

For some reason, javascript.js is not included, even if I specify the absolute path instead of a relative path. Instead, I get a whole copy of my webpage inside a tag in the head section. If I put javascript.js into the doc root folder /var/www/vhosts/project and change the appendFile() path, it works just fine. How can I include javascript outside of doc root?

4条回答
Rolldiameter
2楼-- · 2019-05-23 16:34

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)

查看更多
虎瘦雄心在
3楼-- · 2019-05-23 16:35

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

查看更多
萌系小妹纸
4楼-- · 2019-05-23 16:37

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!

查看更多
女痞
5楼-- · 2019-05-23 16:57

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.

查看更多
登录 后发表回答