Laravel Environment Configuration with Heroku - 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'];
});

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.

Environment issue in Laravel

$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.

Laravel 4.2, Artisan::call() ignoring --env option

I'm building an application that needs to create a new database, perform migrations and seed db data via a web page.
I'm trying to achieve this with the following code in Laravel 4.2. Note, this is within a controller I've setup.
Artisan::call("migrate", array(
"--env" => "production"
));
No matter what environment I pass with the "--env" option, the environment that the migration is run on is the current environment that the site is currently running on. Ie. If I'm running on my local environment, and I run the above, it will execute the migration on the local environment which isn't what I'm looking to do.
If I run the equivalent command php artisan --env=production migrate from the command line, I get the results I'm looking to achieve. For the time being, I'm getting past this via passthru() but I'd like to take advantage of this Artisan facade if I can.
Does anyone know what's going on with this?
This isn't a pleasant way to do it, but it works.
Assuming your Artisan environment is based on $_SERVER['HTTP_HOST'] and you know the HTTP_HOST that will load your environment then you can set it manually before calling start.php
I used this to define the Artisan environment based on the base_url I was using in a Behat profile. That way I could configure fixture my database before running tests.
/**
* #BeforeSuite
*/
public static function runFixtures(SuiteEvent $suiteEvent) {
// Get the environment domain
$parameters = $suiteEvent->getContextParameters();
$baseUrl = $parameters['base_url'];
$urlParts = parse_url($baseUrl);
$_SERVER['HTTP_HOST'] = $urlParts['host'];
// Now call start.php
require_once 'bootstrap/start.php';
// Call Artisan
$stream = fopen('php://output', 'w');
Artisan::call(
'migrate:refresh',
[
'--seed' => true,
],
new StreamOutput($stream)
);
}
--env is the option to specify application's environment when the application is starting. In other words, if you specify --env option, Laravel will use your specified environment instead runs a detecting method in environment detecting method.
So, If you run artisan via CLI with --env option, In start file, artisan can detect --env option from $_SERVER variable, specify the application environment and run your command.
In contrast, when you call Artisan::call(), Laravel will resolve the console application class (Illuminate\Console\Application) and run your command. Because your application was started, then Application just runs your command without detecting environment. More over, latest version of migration command class use application environment to get a database connection
Therefore, when your call Artisan::call() the --env option is completely omitted.
Just my opinion. If you really want to avoid using passthru() function, you can rename the production database connection name in app/config/database.php to unique name e.g. production and set your default database connection to your new name. When you want to migrate production database, just call Artisan::call('migrate', array('--database' => 'production', '--force' => true)) instead of changing the environment.

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