Including PHP files with Laravel framework - php

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!

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.

You can use includes in HTML forget about concatenation
#include('foldername.filename')
#include('filename')

This is another way:
require 'file path';

Related

Laravel 8 – create route alias for image folder inside public directory

I’m in the middle of the process of replicating a framework that I developed in node / react to laravel. Right now, I’m adjusting the main architecture and currently working on a blade master page.
My original idea (Laravel 8 – use blade asset to display image, but loading from resources subfolder) didn’t work, so I’m trying a new approach to set up how I want my asset files to be served.
The assets in question is basically images for layout purposes. I organized the directory like so:
public/app_files_layout
Inside it, I have a bunch of image files that I want to access. The thing is that I don’t want to access like http://localhost:8000/app_files_layout/image-name.jpg. My intention is to access like: http://localhost:8000/images/image-name.jpg, but I want to maintain the directory names I created intact, so it can have a high fidelity architectural organization similar to my framework that I built in other languages.
I figured that I would set up a simple routing logic for it in Laravel web.php file. I followed the suggestion from this stackoverflow question:
https://stackoverflow.com/a/38736973/2510785
However, when I try to access via browser through the following address http://localhost:8000/files-layout-test/image-name.jpg, returned me an error like so:
The requested resource /files-layout-test/image-name.jpg was not found on this server.
I stripped the code just to try to find out what could be wrong, and this is what I did to debug it:
Route::get('/files-layout-test/{filename}', function($filename){
echo 'debug';
});
The strange behavior is that, when I try to access without the file extension (ex: http://localhost:8000/files-layout-test/image-name), it goes through, but I need the file extension to be there.
Any ideas on how I could get this done?
Note: I’m new to Laravel, so the answer may be simple.
Basically for simple stuff like creating a symlink for public/images and public/app_files_layout you can use the built-in storage:link command.
In your config/filesystems.php file, you can define the symlinks you want to create
'links' => [
public_path('storage') => storage_path('app/public'),
public_path('images') => public_path('app_files_layout'),
],
Then you can run php artisan storage:link and it will create all the symlinks defined the links array in config/filesystems.php. No need to create any custom Artisan command. You can read more at Laravel Docs
With the above symlink created you can use the asset() helper to generate the urls for assets which are actually in public/app_files_layout using asset('images/filename.ext').
You can also access public/app_files_layout/image-name.ext at http://localhost:8000/images/image-name.ext once the symlink is created.
However if you want to add some other logic or say you want to get user input for creating symlinks then you can define your own custom Artisan command using the storage:link command as starting point

Should I use .env config file or just php array config?

I need environment specific config file which will not be included in the version control, file with php array seems to be a good choice especially considering that I can define values in desired type and will not need to convert them, but a lot of frameworks and libs use .env files. What are the advantages of .env files and why one should be using them?
I like the way Laravel do it. It implements the config file and env file.
in config/*.php, you'll define things like:
<?php
// config/app.php
return array(
'myconfig' => env('MYCONFIG', 'default')
)
in .env file
MYCONFIG=something
so you'll only need to use config function everywhere.
config('app.myconfig')
btw, it's easy to implement both (isolated or together).
.env files are a standard. It is simple to use and as it sounds like, it is environment independant.
You should definitively use this solution

CodeIgniter + Ion Auth: Ion Auth config file is loaded twice

Explanation:
Using CodeIgniter v3.1.2 with Ion Auth 2
I separated the admin area and user area into 2 different applications to improve security.
I wanted to share config files between two apps so for application_admin/config/ion_auth.php I did this:
require_once(__DIR__.'/../../application_users/config/ion_auth.php');
The script stopped working and after some hours I realized the file is being loaded twice because when I use require instead of require_once everything work's fine.
Questions:
Shouldn't CodeIgniter check and prevent loading files that are already loaded twice? if yes then why is this happening, and if not is it okay if I just use require instead of require_once?
Is my approach the correct way of sharing config files and helpers between multiple applications in a single CI install?
Edit:
I put the require_once in the admin/config/ion_auth.php to include user/config/ion_auth.php so basically I'm putting the content of that config file in this config file "only when CodeIgniter itself loads this config file".
Also even if I put the normal ion_auth config file without any require/include etc, it is still loaded twice (I noticed by putting a simple echo 'loaded<br>'; in the end of config file.)
Shouldn't CodeIgniter check and prevent loading files that are already
loaded twice? if yes then why is this happening, and if not is it okay
By making direct call to require or require_once you are bypassing Codeigniter (CI) so it has no way of knowing what you have or have not included.
if I just use require instead of require_once? Is my approach the
correct way of sharing config files and helpers between multiple
applications in a single CI install?
Loading a config file in CI should be done using the Config class. If you dig into the source code for Ion Auth and look at __construct in Ion-auth_model.php you will find heavy use of the config class to read the config file and access various config items. It a good example of the "CI way" to handle configuration.

How to get config data in laravel in a subfolder

I have the Laravel Administrator Plugin and I set up a setting administrator page that is located in:
app/config/administrator/settings/site.php
The application can store the data, but when i try to get some data in one of my controllers like this:
Config::get('administrator.settings.site')
I can get a returned value... Always null or array 0.
How I can get those configuration values?
Solution:
You can use a file path rather than dot notation for file paths:
Config::get('administrator/settings/site.variable_name');
Some Explanation
Dot notation is used to get variables inside the config array within a file, rather than to denote file paths.
Therefore you can use Config::get('administrator/settings/site.variable_name'); to get $variable_name from administrator/settings/site.php, or ENV_DIR_NAME/administrator/settings/site.php depending on your environment.
Directories within in app/config are read as environments. For example app/development/database.php will work with Config::get('database.whatever') when you're in your "development" environment while app/production/database.php will be read with Config::get('database.whatever') when you're in the "production" environment.
Laravel falls back to the config/config/*.php file when no environment-specific file is present.
Note
I believe that admin package has a config file in app/config/packages/frozennode/administrator/administrator.php which the package would like you to use for it's settings. That should be available using this convention: Config::get('package::file.option');
I ran into a very similar problem when using this Laravel-Excel package.
The solution I needed was slightly different to the solution above. I needed to take advantage of Laravel's config overriding feature to have one config setting for normal execution and a different one for testing. Specifically, I normally wanted Excel files to be stored in storage/excel and I wanted them to be put in storage/testing/excel under testing.
The technique of using a slashed path didn't work because it points to a specific file so didn't respect the different environments:
$directory = Config::get('packages/maatwebsite/excel/export.store.path');
The solution was to use a package prefix so that the config loader would respect the environment:
$directory = Config::get('excel::export.store.path');
I'm not exactly sure where the excel shorthand name comes from but I suspect it's something to do with the Excel facade in the package exposing itself as 'excel'.

PHP extending requires on files

I have a problem where I would like assistence with.
Currently on working on a new project for myself and I am trying to start with a good foundation.
I have build the following file structure (relavant part):
Class/
Class/class.filename.php
Class/class.etc.php
Func/
Func/func.filename.php
Func/func.etc.php
Config/config.php
index.php
In my config file all classes and functions are included with the require_once function (I loop alle files and directories inside func and class). In the folder class the file for firePHP is located which I include and then setup in the config.php.
In my config.php and index.php I can call this log function perfectly, but when I use it in one of the func.filename.php or class.filename.php it errors. The child (func/class) is not seeing the other included functions within config.php.
Hope somebody can help me out with this.
You func.filename.php file needs to "require_once" the file that contains the log function.
Another solution that may work for you (since you have this structure) is to have all your required files in config.php (included with require_once(<file>)). This seems to work. Then, when other files required any other function for other files, just require_once('config.php').
It is best to only include what you need, when you need it. Why spend the time loading 50 different classes/functions/snippets when you might only need 2 or 3 for the script at hand? Paring out unnecessary include statements can increase the performance of your scripts considerably.
Make use of the __autoload() function for classes. It is just super.
Chain include_once() or require_once() through your various files so that grabbing one file that you need for a given job automatically gets its own dependencies.

Categories