2

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?

1
  • 3
    You can't. The browser needs something inside the doc root to access. You could create a PHP file that passes through the script, or use Alias if you have access to your server's configuration Commented Jan 11, 2011 at 15:03

4 Answers 4

2

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the clarifications. However I try to avoid symlinks, because I develop my projects using Xampp on Windows 7. I'll look into my server configuration and see if I can use an Alias as suggested above.
2

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.

2 Comments

What about writing something like appendExternalFile()?
appendFile() only outputs <script> tags using the path you specify. The webserver will not serve files outside of the document root.
1

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

Comments

1

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!

Comments

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.