2

We run a main website which is not made in Laravel. A specific script on this website however is made in Laravel. We need this script (or to be exact, a specific view inside of it) to fetch some resources from the main website (PHP files which mainly include HTML). When, inside the Laravel application, we try to include these resources, we can not - as they don't exist in the Laravel project workspace. This results in an error that the file does not exist. Attempting to climb out of the project (../../../file.php) does not seem to help either.

We're including it this way:

<?php
  include '~/template/nav.php';
?>

We don't wish to include this file into the actual Laravel project, as that would require having to update it twice to ensure they remain equal. Is there any way for us to include this "external" file in our view? Every bit of research done seems to suggest adding it into the project, which would just cause twice the amount of administration on updates.

Cheers!

5
  • Have you tried creating a symlink in laravel's public folder and include the symlink with include public_path('nav.php'); Commented Aug 18, 2015 at 18:11
  • @michael I have not. I guess that would work - but I'm still curious on whether or not we can include external files. Commented Aug 18, 2015 at 18:13
  • You can't include non project files, not even in a non laravel project unless you set open_basedir in php.ini to allow php to access files outside of the document root. You could do this with your laravel just the same way, but you'll then have to provide the full path name, starting from your servers root dir. /var/www/html/... e.g. Commented Aug 18, 2015 at 18:42
  • @michael Non-laravel projects basically allow me to include php files from anywhere inside a folder that is accessible by a PHP interpretor (public_html in this case). In Laravel, this does not work, as Laravel refuses to read from anything outside of its own subdirectory. I tried a symlink as well, which also seemed to fail. EDIT Actually, the symlink worked - but won't do in this case, as it will now read config variables from the wrong scope. Guess I have no choice other than to create a copy. Commented Aug 18, 2015 at 19:06
  • @MarcusFrölander Laravel is in PHP, so your argument is moot. You should re-think your entire structure, or use a package. Commented Aug 20, 2015 at 7:47

1 Answer 1

1

Just faced same problem. My Laravel Project is an API that need's to include outside php file. So what I did was specify entire path:

include($_SERVER['DOCUMENT_ROOT'].'/../../my_example_file.php');

$_SERVER['DOCUMENT_ROOT'] will lead you to Laravel's public folder.

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

1 Comment

In that case, I'm giving you the accepted answer since you found a way to get it to work! Better late than never :)

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.