31

I am trying to "include" a php script into one of my views (landing.blade.php).

The script is in:

/laravel-master/public/assets/scripts/config.php

When I try to include this code in the view:

<?php include_once('/assets/scripts/config.php'); ?>

I get the error: include_once(/assets/scripts/config.php): failed to open stream: No such file or directory

This is on localhost using MAMP. I'm not sure if there is a different set of rules I need to use with Laravel 4 to include a php file. Thank you for your help!

3
  • /assets/scripts/config.php means it's at the root of your OS. Use an absolute path or change it to relative. Commented Jun 18, 2013 at 0:49
  • Could you expound on that? What would the relative path look like? ../assets/scripts/config.php ? Commented Jun 18, 2013 at 0:59
  • One often runs into this error, and to quickly troubleshoot it, follow these steps : stackoverflow.com/a/36577021/2873507 Commented Apr 12, 2016 at 15:17

3 Answers 3

50

First, it's not really recommended that you keep your PHP files in the public directory, they should be kept inside the app folder. I'd suggest you create a folder inside app, something like includes and put your files there. Then, you include it, do:

include(app_path().'/includes/config.php');

Although, since it looks like you're trying to load some configuration files, I'd recommend you also check out Laravel's own way of handling configurations. For instance, if you created a myapp.php file inside the app/config folder, Laravel would handle it automatically for you, as long as you'd have some key-value pairs, like this:

<?php

return [
    'name'     => 'Raphael',
    'gorgeous' => true
];

You could then retrieve these values using the Config class:

Config::get('myapp.name'); // Raphael

This is a better solution because you can also take advantage of Laravel's environment configuration.

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

11 Comments

I am trying to load this configuration file for a Facebook login. Contains appID, secret, etc... Does this change your answer at all?
Well, if you can't change the code that will use the configuration file, then it renders the 'using Laravel's built in configuration stuff' invalid, but the rest still counts.
Where do I put the: $app = app(); include($app['path.app'].'/includes/config.php'); in my view?
Better yet, you can use include(app_path().'/includes/config.php'); in your view.
I have started a troubleshooting checklist for this frequent error here : stackoverflow.com/a/36577021/2873507
|
2

You can use includes in HTML forget about concatenation

@include('foldername.filename')

@include('filename')

Comments

1

This is another way:

require 'file path';

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.