2016年11月11日 星期五

How to Setup Multiple Environment for Laravel 5 Developers Way


Hello Guys, Today i will teach you on how to setup multiple environment in using Laravel 5 Framework.

Update Codes: April 02, 2015

  • I put laravel 5.1 version for loading the env file.
  • Dotenv::load(__DIR__ . '/../', '.' . getenv('APP_ENV') . '.env'); 

Update Codes: Feb 05, 2015

  • Please check the new code for laravel 5.2 for loading using DotEnv

  •  
If you just heard Laravel 5 for the first time, take a look on their website you will see the advantage of using it and be A Web Artisans.
Important benifits using Laravel 5.
  • Expressive, beautiful syntax.
  • Tailored for your team.
  •  Modern toolkit. Pinch of magic.
  • Love beautiful code
  • And Many More…

So lets get back to business, First thing to do is to checkout a new laravel 5 using composer.
You can take a look in the documentation here: Laravel 5 Installation.
After you setup everything, from installation of Homestead and Setting up new Laravel 5 virtual domain..
Follow this instructions:
1. Check if you have .env file (it should be automatically created by laravel)
  • REMOVE all the contents from here, and just put: local or production (whatever environment you want). This will be the one that will be changed when you want to switch to another environment. :D
2. Create new Environment file let say you have your local and production enviroment.
  • Create a file with the name of: .local.env
  • Create a file with the name of: .production.env
3. Add default environment value.
  • For Local Environment (.local.env file)
    • APP_ENV=local

  • For Production Environment (.production.env file)
    • APP_ENV=production
4. Create new php file and named it, environment.php, save it into this folder: app/bootstrap/environment.php
– Inside of this file we will do the magic. Insert this snippet code.

5.  This snippet code, use, putenv php function to set APP_ENV and then using the Dotenv package by vlucas. I’m loading the file that we created and base on the .env file we can determine which environment should we use.
6. Include your environment.php file in bootstrap file. Paste it inside your bootstrap/app.php file.
6. After that you can check which environment is being loaded using:
7. I output the current environment in my Laravel 5 Welcome Page, You can see my screenshots below:
How to Setup Multiple Environment for Laravel 5 Developers Way
How to Setup Multiple Environment for Laravel 5 Developers Way
How to Setup Multiple Environment for Laravel 5 Developers Way
How to Setup Multiple Environment for Laravel 5 Developers Way

from : http://developers.ph/laravel-framework/laravel-5/how-to-setup-multiple-environment-for-laravel-5-developers-way/

沒有留言:

wibiya widget