Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including PHP files with Laravel framework

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!

like image 936
user1072337 Avatar asked Jun 18 '13 00:06

user1072337


People also ask

Can I use normal php in Laravel?

laravel does not get in the way of using normal PHP functions, so this always works.

What is php artisan storage Link?

php artisan storage:link. Once a file has been stored and the symbolic link has been created, you can create a URL to the files using the asset helper: echo asset('storage/file.txt'); You may configure additional symbolic links in your filesystems configuration file.

How do I get files in Laravel?

If you have file object from request then you can simply get by laravel function. $extension = $request->file->extension(); dd($extension); If you have file object from request then you can simply get by laravel function.


2 Answers

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.

like image 121
rmobis Avatar answered Sep 23 '22 13:09

rmobis


You can use includes in HTML forget about concatenation

@include('foldername.filename')  @include('filename') 
like image 25
Adam Info Avatar answered Sep 22 '22 13:09

Adam Info