menu

Phalcon - Add ENV and function dd


Pada tutorial kemarin kita sudah membuat koneksi database pada phalcon dan instalasi composer, tutorial kali ini kita akan menambahkan file .env dan instalasi debuger pada phalcon.

Oke, langsung saja kita mulai experimentnya.
Pertama, edit file config.php pada path app/config/ menjadi seperti ini,
<?php
/*
 * Modified: prepend directory path of current file, because of this file own different ENV under between Apache and command line.
 * NOTE: please remove this comment.
 */
defined('BASE_PATH') || define('BASE_PATH', getenv('BASE_PATH') ?: realpath(dirname(__FILE__) . '/../..'));
defined('APP_PATH') || define('APP_PATH', BASE_PATH . '/app');
// defined('BASE_URL');

if (!function_exists('env')) {
    /**
     * Load start
     */
    include_once APP_PATH . "/config/start.php";
}

return new \Phalcon\Config([
    'database' => [
        'adapter'     => 'Mysql',
        'host'        => env('DB_HOST'),
        'username'    => env('DB_USER'),
        'password'    => env('DB_PASS'),
        'dbname'      => env('DB_NAME'),
        'charset'     => 'utf8',
    ],
    'application' => [
        'appDir'         => APP_PATH . '/',
        'controllersDir' => APP_PATH . '/controllers/',
        'modelsDir'      => APP_PATH . '/models/',
        'migrationsDir'  => APP_PATH . '/migrations/',
        'viewsDir'       => APP_PATH . '/views/',
        'pluginsDir'     => APP_PATH . '/plugins/',
        'libraryDir'     => APP_PATH . '/library/',
        'cacheDir'       => BASE_PATH . '/cache/',

        // This allows the baseUri to be understand project paths that are not in the root directory
        // of the webpspace.  This will break if the public/index.php entry point is moved or
        // possibly if the web server rewrite rules are changed. This can also be set to a static path.
        'baseUri'        => preg_replace('/public([\/\\\\])index.php$/', '', $_SERVER["PHP_SELF"]),
    ]
]);

Setelah itu, edit file composer.json pada path root menjadi seperti ini,
{
 "name": "codedoct",
 "type": "project",
 "authors": [
        {
            "name": "Mihawk"
        }
    ],
 "require": {
  "guzzlehttp/guzzle": "~6.0"
 },
 "require-dev": {
        "symfony/var-dumper": "2.7.*",
        "vlucas/phpdotenv": "~2.0"
    }
}
Pada code composer diatas kita menbahkan plugin untuk env dan debuger.

Selanjutnya, buat file start.php pada path app/config/ dan isikan code berikut,
<?php
/**
 * Start loader configuration
 */

// Use composer autoloader to load vendor classes
require_once BASE_PATH . '/vendor/autoload.php';

/**
 * Environment variables
 */
$dotenv = new Dotenv\Dotenv(BASE_PATH . '/');
$dotenv->load();

/**
 * Include helpers
 */
include_once BASE_PATH . '/helpers.php';

/*
 * Debug
 */
if (env('APP_ENV') == 'dev') {
    $debug = new \Phalcon\Debug();
    $debug->listen();
}
Dapat dilihat dari code start diatas, kita menambahkan path vendor yang sudah kita build pada tutorial sebelumnya.

Kemudian buat file helpers.php pada path root dan isikan code berikut,
<?php

/**
 * Get .env value and return second parameter as default value
 */
function env($name, $default = null)
{
    return (getenv($name)) ?: $default;
}

/**
 * Current shortcode only available on dev env
 */
if (env('APP_ENV') == 'dev')
{
    /**
     * Simple function for better debugging view
     */
    class Dumper
    {
        /**
         * Dump a value with elegance.
         *
         * @param  mixed  $value
         * @return void
         */
        public function dump($value)
        {
            $dumper = 'cli' === PHP_SAPI ? new \Symfony\Component\VarDumper\Dumper\CliDumper : new \Symfony\Component\VarDumper\Dumper\HtmlDumper;
            $dumper->dump((new \Symfony\Component\VarDumper\Cloner\VarCloner)->cloneVar($value));
        }
    }

    if (!function_exists('dd')) {
        /**
         * Dump the passed variables and end the script.
         *
         * @param  mixed
         * @return void
         */
        function dd()
        {
            array_map(function ($x) {
                (new Dumper)->dump($x);
            }, func_get_args());
            die(1);
        }
    }
}
Dapat dilihat dari code helpers diatas kita menambahkan function dd yang akan digunakan untuk mendebug code.

Terakhir buat file .env pada path root dan isikan code berkut,
APP_ENV="dev"

DB_HOST="localhost"
DB_USER="root"
DB_PASS="root"
DB_NAME="phalcon-experiment_312"

Sehingga struktur file yang baru akan tampak seperti ini,


===DONE!===

2 komentar

Hi there, this weekend is good in support of me, since this point in time i am reading this wonderful educational paragraph here at my home. gmail sign in