Laravel configuration usage for different servers in pure php file - laravel

I am new in Laravel and want to know such problem
I am running an web application on several server using Laravel.
But I have encountered with an issue.
When there is modification for the project, I need to sync with git on several servers.
But it has different settings for each server (eg: DB name, DB password...)
I have set it manually because I couldn't use .env or configuration file since the file is just pure php file.
The issue I want to solve is how can I get Laravel configuration data from pure PHP file(not controller or whatever).
It will be thankful if someone teach me solution.

You asked:
how can I get Laravel configuration data from pure PHP file
The answer would be this:
You just make some file in config/ folder of laravel app (or use the existing file like config/app.php)
You make an array of your key value pairs
<?php
return [
'some_key' => 'some_value',
...
and you simply call it with this code where ever you need it:
config('config_file.key');
for example
config('app.name');
would give you Laravel by default.

Related

Why calling env() function directly in controllers does not work?

Supporting Laravel 6.20 app, I see a lot of env() methods called in the controllers.
It does not work on my side, so I made a wrapper in the config/app.php file, and using it made it work.
And I have to make a lot of similar changes in the app to make it work properly...
But looks like that env() worked for my client on live and developers server.
I know that calling env() in controllers directly is a bad way but wonder why it worked on servers?
Are there some common PHP configurations?
Thanks!
you have probably cached your configurations by running php artisan config:cache
according to the doc
This command will combine all of Laravel's configuration files into a single, cached file, which greatly reduces the number of trips the framework must make to the filesystem when loading your configuration values.
and then
If you execute the config:cache command, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function for .env variables will return null.
you have to just run php artisan config:clear and this will clear the cache file. you will be able to get the env values again.
but yeah don't use any values directly from .env files. there are some issues. to get more performance, cache the configs. so wrap the env value in a config key like in config/app.php
'name' => env('APP_NAME', 'Laravel')
you are saying that use the APP_NAME key from the env file as your laravel app name and if there's no APP_NAME key or value in env use Laravel as default.
secondly if you make some requests within a very short period of time, like page loads and you are calling some xmlhttp requests at the same time, there's possibility of not getting the env values (returns null) if you have to use any. this is a php bug. so read env values from config files with a default value for performance and for avoiding bug.

Whats is Production enviroment in Laravel?

I am curious to know what the APP_ENV entry is used for in Laravel environment files. Is it just for my own usage so I can detect it in code? If I create a fresh Laravel app and change APP_ENV to production what will it change under the hood? Nothing?
Thanks!
Typically you would have a different .env file on each server. It is up to you if you would like different parts of your app to work differently in different environments. The .env file is usually used so that your code can just grab values with the env helper function. This way when you change environments like switching from a test API key to a live API key for example, you can just edit the .env file and not touch the rest of your code.
https://laravel.com/docs/5.5/configuration#environment-configuration

restrict access on public folder in laravel 4.2

im fairly new to laravel and im currently working on a project that needs to store files in the server. What im doing is store it under public/docs the problem is the files under that directory is accessible all the time given that they know the url to the certain file. for example even if they are logged out if they know this url http://localhost/dummy/public/uploads/docs/2016-06-02-10-52-43-oth-content-inventory-and-planning-spreadsheet.pdf they would be able to access it what im thinking is to do something like this
in my routes i would do something like
Route::any('uploads/docs/' , 'go to some controller to check if user can acces');
is this possible? thanks in advance!
it's called public for a reason...
you should use storage instead (laravel 5) in storage you dont restrict access, actualy the opposite, you grant access to the file .
try this for laravel 4.2 (move the file after upload like this to a storage folder)
Input::file('file')->move(__DIR__.'/storage/',Input::file('file')->getClientOriginalName());
});
Anyway ,the conclusion is that you can't restrict access (you shouldn't even if there is a way) because it's meant to be public ! so you should use storage folder(or whatever its called in laravel 4)

Lumen file cache driver

I'm in Lumen, inside a Controller, and I would like to cache a computation's result in a simple and easy way, without using database or external services, so I was looking for save the cache in the filesystem. In Laravel's documentation there is cited the file driver:
By default, Laravel is configured to use the file cache driver, which
stores the serialized, cached objects in the filesystem.
And I can see it, configured as Default Cache Store, inside config/cache.php.
In Lumen's documentation i can't see anything about the file driver and I find nothing like the file cache.php inside Lumen installation.
So my question is if I can use the file cache driver in Lumen (by setting CACHE_DRIVER=file) or if it is discouraged, not supported, not implemented or something else?
In Lumen in .env.example you have by default:
CACHE_DRIVER=memcached
So all you need is to change filename from .env.example to .env and set
CACHE_DRIVER=file
If you read Caching in Lumen you'll see in example:
$value = Cache::store('file')->get('foo');
so file driver is supported by Lumen.
If you also read Lumen Configuration you can read here that you can copy configuration files you need (in case you need them) and load them manually. You can see default Luman cache config file here: https://github.com/laravel/lumen-framework/blob/5.1/config/cache.php

Using php scripts on my views in Laravel 4

I am using the Laravel 4 framework, and I am trying to set up the Facebook authentication system. I have an authentication system I had set up on another site (not using a framework) that used a config.php and process_facebook.php file. I am trying to implement this config.php file into my views. So far, I am including the files in a folder called "includes", within my "app" folder. I am trying to use the following code to implement it:
$app = app();
include($app['path.app'].'/includes/config.php');
My question is, where in the view do I put this code? Do I have to use php tags? (I am using the blad functionality). Your help is appreciated.
Laravel is an MVC framework, the purpose is to organise your code and clean your views. So this shouldn't be in your view.
I think the best way should be :
Create a facebook.php file in the config folder wich contains all your facebook configuration (read http://laravel.com/docs/configuration)
Create a folder named services, helpers or includes (as you want) and put process_facebook.php inside (I bet it contains the methods to deal with facebook API).
Add two lines of configuration to include this new folder
Like that :
// composer.json
"autoload": {
"classmap": [
[...]
"app/services",
]
},
// start/global.php
ClassLoader::addDirectories(array(
[...]
app_path().'/services',
));
Then, you can use your facebook class or methods all over your app.
The route you are taking to include configuration files is not recommended, but it is possible. Please see information about Laravel Configuration Files.
You should be able to use the following in your view:
<?php include(app_path().'/includes/config.php'); ?>
As it is a configuration file, it would be better to use require() instead of include().
In addition, it would also be better to include the file in the necessary controller(s).

Resources