Environment issue in Laravel - laravel-4

$env = $app->detectEnvironment(array(
'local' => array('*localhost*'),
'test' => array('chan.app'),
));
This is how I set in boostrap/start.php, and I set ip in hosts file
127.0.0.1 localhost
127.0.0.1 chan.app
No matter I type http://localhost/ or http://chan.app, App::environment() always reveal production, therefore I can't change database config for it.

Open your CMD (Mac, Linux, and Windows) and type the command:
hostname
this will return the name of your computer. Then use that name:
$env = $app->detectEnvironment(array(
'local' => array('ComputerName'),
));

Because of security issues, the use of url domains in environment is forbidden. Laravel says to use hostnames instead.
This is why I doubt, that Laravel would recognise (detect) your configuration correctly, as both are on the same machine.
From the 4.2 Upgrade Guide:
Environment Detection Updates
For security reasons, URL domains may no longer be used to detect your
application environment. These values are easily spoofable and allow
attackers to modify the environment for a request. You should convert
your environment detection to use machine host names (hostname command
on Mac, Linux, and Windows).
EDIT
Let's say, you want a local and a live environment.
1. Create folders for each configuration:
Create a folder local & live inside /app/config/
Inside each of those folders you create the config file(s) you wish to override from /app/config/,
For example, in your live (production) environment, you don't want to have the debug option activated.
Create a file app.php in the /app/config/live folder.
Inside, you'll just return the desired options to override, as defined in the original /app/config/app.php.
return array('debug' => false);
In the local environment, 'debug' would be set to true, for development.
2. Add the environment to the framework:
In your /bootstrap/start.php file:
$env = $app->detectEnvironment(array(
'local' => array('local-machine-name'),
'live' => array('yourdoamin.com')
));
That's the important part:
- Development (local): --> machine name
- Production: --> root-url (yourdomain.com) which represents the "machine" name
See the docs about environment config for further information.

Related

Laravel set and detect enviroment using .env file

I am working on Laravel 4.2.
I've three environments - Local i.e. my localhost on WAMP, dev i.e. development environment on Ubuntu digital ocean and production - i.e. Production environment on Ubuntu Amazon.
Now, each time I deploy the code on each environment, I am forced to manually setup host name, database name, user and password.
I go through the Laravel documentation as well as some searching and finally I've created 3 directories inside app/config/ directory namely - local, dev and production. I've placed database.php inside each directory with the said database connection settings.
Now I've created a evn.local.php file in the root directory for local environment as below :
<?php
return [
'STRIPE_SECRET' => 'my-secrete-test-stripe-key',
'APP_ENV'=>'local'
];
And also have changed $app->detectEnvironment function of \bootstrap\start.php as :
$env = $app->detectEnvironment(function(){
return getenv('APP_ENV') ? : 'local';
});
It is working fine for local but I am not sure will it work for dev and production or not.
Also, in App/Config/Services.php I've placed code :
'stripe' => array(
'model' => 'User',
'secret' => $_ENV['STRIPE_SECRET'],
)
and If I use like
Config::get('services.stripe.secret')
it is giving me the secrete key I've placed in evn.local.php but if I use the same to get the environment in \bootstrap\start.php as :
$env = $app->detectEnvironment(function(){
return $_ENV['APP_ENV'];
});
it is not working. Gives error Undefined index: APP_ENV.
Can anyone guide me what am I missing? Or am I handling it wrong way?

PHP Laravel Server Environment Configuration

I am new with laravel and I am confused on how to setup my server environment configuration. So here is a sample code I've seen in some tutorials, but still I am not satisfied that this would be flexible enough specially when my other co-developers use it.
$env = $app->detectEnvironment(array(
'local' => ['127.0.0.1', gethostname()],
'production' => ['ipforproductionhere'] ));
You can put in local names of your and other developer PC for example:
'local' => ['yourpcname', 'yourcoworkerpcname', 'yourothercoworkerpcname'],
And of all you when developing on those machines will work in local environment.
EDIT
You can also use other type of environment detecting for example:
$env = $app->detectEnvironment(function(){
if (!isset($_SERVER['HTTP_HOST']) ||
strpos($_SERVER['HTTP_HOST'],'.') === false) {
return 'local';
}
return 'production';
});
Now assuming each developer create virtualhost without a dot and uses for example http://myproject as domain it will use local environment and if dot will be found in HTTP_HOST it will use production environment.

Laravel environment files for local and production possibly not working as intended?

In Laravel 4, I'm setting up my local development machine (my laptop) and then I have my production server.
In my /bootstrap/start.php
$env = $app->detectEnvironment(array(
'local' => array('MacBook-Pro-2.local'),
'production' => array('ripken'),
));
I then created a .env.local.php in my root directory with the following:
<?php
return [
// Database Connection Settings
'db_host' => '127.0.0.1',
'db_name' => 'censored',
'db_user' => 'censored',
'db_password' => 'censored'
];
That part works. However, I went to my production server and created the same exact file, called it .env.production.php and it doesn't load the variables from that file. I don't know what made me think to do it but I renamed that file .env.php without "production" in the name and it works.
My question therefore is, why wouldn't .env.production.php work? I thought that is what the good folks over at Laravel wanted me to name my various environment files.
Production is the 'default' ruleset, so it is a bit special. Laravel will look for the "base" or "default" rules when loading production, rather than the actual name production.
So you can change
$env = $app->detectEnvironment(array(
'local' => array('MacBook-Pro-2.local'),
'production' => array('ripken'),
));
to
$env = $app->detectEnvironment(array(
'local' => array('MacBook-Pro-2.local'),
));
That way anything that is not "MacBook-Pro-2.local" is going to be production automatically. Then just use the default .env.php for the production settings.
Every other env needs to be explicitly defined - such as .env.local.php and .env.testing.php etc

Laravel Environment Configuration with Heroku

I am writing my code on Nitrous.IO using Laravel I configured my start.php to the following
$env = $app->detectEnvironment(array(
'local' => array('harrenhal-php-95199'),
'staging' => array('*.herokuapp.com'),
));
I tried this format I also tried the hostname itself both seems not to working. is there a specific practice to do so?
Once I run hostname on the shell in heroku I get this result "9e7831e0-284c-48b8-88a4-3afbbbac0b35" which changes over time.
the problem php don't detect herokuapp.com so the staging environment doesn't work
From the Laravel docs
If you need more flexible environment detection, you may pass a
Closure to the detectEnvironment method, allowing you to implement
environment detection however you wish
$env = $app->detectEnvironment(function()
{
return $_SERVER['MY_LARAVEL_ENV'];
});

How to disable Kint in local environment under Laravel 4

I'm using Kint via Composer in Laravel 4 by loading kint first in composer.json so that dd() is defined by kint, not laravel (suggested here).
I want to leave debug calls in my app, and disable Kint if not in the local environment. I'm successfully using config overrides for Anvard using the following structure:
/app/config/local/packages/provider/package_name/overridefile.php
Unfortunately, this is not working for Kint with the following structure:
/app/config/packages/raveren/kint/local/config.php or
/app/config/packages/raveren/kint/local/config.default.php
The Kint documentation states:
You can optionally copy the included config.default.php and rename to config.php to override default values…
…which works for me (/vendor/raveren/kint/config.php)
How do I achieve this:
without editing a file in the /vendor/ directory that will get overwritten by composer
so that kint is only enabled in the local envirnoment
I've also tried adding the following to a helpers.php file which is called before composer in /bootstrap/autoload.php as suggested here:
<?php
isset( $GLOBALS['_kint_settings'] ) or $GLOBALS['_kint_settings'] = array();
$_kintSettings = &$GLOBALS['_kint_settings'];
/** #var bool if set to false, kint will become silent, same as Kint::enabled(false) or Kint::$enabled = false */
$_kintSettings['enabled'] = false;
unset( $_kintSettings );
(but no dice :)
Any suggestions? TIA!
I'm not familiar with kint but checked the documentation and found that, to disable kint output, you may use (in runtime)
// to disable all output
Kint::enabled(false);
In Laravel you can check the environment using
$env = App::environment();
if($env == 'your_predefined_environment') {
Kint::enabled(false);
}
To configure your environment, you may check the documentation.
Update : I've setup my local environment as givel below (in bootstrap/start.php)
$env = $app->detectEnvironment(array(
'local' => array('*.dev'),
));
And in my local machine, I've setup a virtual mashine which has laravel4.dev as it's base url, so if I visit the app using laravel4.dev or laravel4.dev/logon then I can check the environment in my BaseController.php and it detects the local environment because of .dev
public function __construct()
{
if(App::environment() == 'local') {
// do something
}
}
In your case, I don't know where is the first debug/trace you used to print the output, so you should keep the environment checking and disabling the Kint code before you use any debug/trace but you may try this (if it works for you) but you can check the environment in your filter/routes files too.
Hmm.. I'm not sure if this is the ideal way to do it, but this works, and seems Laravel'ish:
// top of app/start/global.php
Kint::enabled(false);
and
// app/start/local.php
Kint::enabled(true);
(assuming you've got a local environment defined: see #TheAlpha's answer for more info)
http://laravel.com/docs/lifecycle#start-files

Resources