Including javascript file outside of doc root using zend framework

I am trying to include javascript file in phmtl view script file using zend structure. Both the javascript file and the phtml file are part of the php library and are 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 is added to the PHP path using set_include_path. In viewscript.phtml, I use the following line to enable javascript.js.

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

For some reason javascript.js does not turn on, even if I specify an absolute path instead of a relative path. Instead, I get a whole copy of my webpage inside the tag in the chapter section. If I put javascript.js in the root doc / var / www / vhosts / project folder and changed the appendFile () path, it will work fine. How to enable javascript outside the doc root?

+2
source share
4 answers

Based on the previous questions you asked, I think your catalogs are something problematic for you. here is a functional and safe example or directory organization for the 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 - / var / www / project / htdocs . Here we can find the index.php file, the only php file available for sharing.

In htdocs / js and htdocs / css you can put some of your js and css project files. And then you had a problem with the css and js files of your external php libraries, which are now completely outside the web root.

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

  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 this for all external libraries that have files that should be in the root directory of the document. Thus, the base url for js files my-lib is / js / my-lib /.

Do not be afraid to use symbolic links (connections in windows), you can even store them in a subversion repository. Just make sure your apache configuration allows you to use symbolic links (Options + FollowSymlinks)

+2
source

The path specified in appendFile() refers to the root of the site document (for example, to your "public" folder). It will not pick up php include_path.

You can transfer the js file to the doc root directory, create a symbolic link to it in the doc root, or you can read the file with php and display its contents as a <script> .

+2
source

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

JavaScript is a client language, it runs on the user's computer and is interpreted there, so the user must have access to the file, therefore, it must be inside the root path, since the user (client) should not have access to your application.

You can save the PHP file in the root of your document and use it to get JS:

getJS.php (stored 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 can include switches to include different JS files or what you need, I have not tested this for functionality, but when I click on the file, the file receives the contents of the JS file.

Note. . If this is for security reasons, not much is needed to get the contents of the file that the user wants!

+1
source

form your path, I can say that you are using linux so you can use symlink as follows:

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

for this you can add files:

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

and tada its done

+1
source

Source: https://habr.com/ru/post/1335099/