DART, AngularDART and Zend Framework 2 [part 2: db connection]

By | 2014/05/13
This entry is part 2 of 4 in the series DART, AngularDART and Zend Framework 2

Now it is time to start some real development..
Let’s do the backend (webservices, data source access and so on..)
So from the beginning:

  1. Download composer.phar (for example from https://getcomposer.org/download/) to dir you want to have the webpage in
  2. create composer.json file with content
        "name": "zendframework/skeleton-application",
        "description": "Skeleton Application for ZF2",
        "license": "BSD-3-Clause",
        "keywords": [
        "homepage": "http://framework.zend.com/",
        "require": {
            "php": ">=5.3.3",
            "zendframework/zendframework": "2.*",
            "doctrine/annotations": ">=1.0"
  3. go to that directory (with composer.phar in it) and execute from command line:
    php composer.phar install
    this should create vendor directory
  4. dir structure in this framework seems like important one (sometimes it can help to not make too much mess):
    > config
    -> autoload
    > module
    -> Application
    --> config
    --> src
    --> view
    ---> error
    ---> layout
    -> Recipes
    --> config
    --> src
    ---> Recipes
    ----> Controller
    ----> Form
    ----> Model
    --> view
    > public
    -> css
    -> fonts
    -> img
    -> js
  5. next step would be: creation of the init_autoloader.php in the main dir of our webpage (same place where you did previous steps) with content:
     * Zend Framework (http://framework.zend.com/)
     * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
     * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
     * @license   http://framework.zend.com/license/new-bsd New BSD License
     * This autoloading setup is really more complicated than it needs to be for most
     * applications. The added complexity is simply to reduce the time it takes for
     * new developers to be productive with a fresh skeleton. It allows autoloading
     * to be correctly configured, regardless of the installation method and keeps
     * the use of composer completely optional. This setup should work fine for
     * most users, however, feel free to configure autoloading however you'd like.
    // Composer autoloading
    if (file_exists('vendor/autoload.php')) {
        $loader = include 'vendor/autoload.php';
    $zf2Path = false;
    if (is_dir('vendor/ZF2/library')) {
        $zf2Path = 'vendor/ZF2/library';
    } elseif (getenv('ZF2_PATH')) {      // Support for ZF2_PATH environment variable or git submodule
        $zf2Path = getenv('ZF2_PATH');
    } elseif (get_cfg_var('zf2_path')) { // Support for zf2_path directive value
        $zf2Path = get_cfg_var('zf2_path');
    if ($zf2Path) {
        if (isset($loader)) {
            $loader->add('Zend', $zf2Path);
        } else {
            include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
                'Zend\Loader\StandardAutoloader' => array(
                    'autoregister_zf' => true
    if (!class_exists('Zend\Loader\AutoloaderFactory')) {
        throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
  6. now the first of config files (yeap.. there will be quite a lot of config files).. /config/application.config.php :
    return array(
        // This should be an array of module namespaces used in the application.
        'modules' => array(
        // These are various options for the listeners attached to the ModuleManager
        'module_listener_options' => array(
            // This should be an array of paths in which modules reside.
            // If a string key is provided, the listener will consider that a module
            // namespace, the value of that key the specific path to that module's
            // Module class.
            'module_paths' => array(
            // An array of paths from which to glob configuration files after
            // modules are loaded. These effectively override configuration
            // provided by modules themselves. Paths may use GLOB_BRACE notation.
            'config_glob_paths' => array(
            // Whether or not to enable a configuration cache.
            // If enabled, the merged configuration will be cached and used in
            // subsequent requests.
            //'config_cache_enabled' => $booleanValue,
            // The key used to create the configuration cache file name.
            //'config_cache_key' => $stringKey,
            // Whether or not to enable a module class map cache.
            // If enabled, creates a module class map cache which will be used
            // by in future requests, to reduce the autoloading process.
            //'module_map_cache_enabled' => $booleanValue,
            // The key used to create the class map cache file name.
            //'module_map_cache_key' => $stringKey,
            // The path in which to cache merged configuration.
            //'cache_dir' => $stringPath,
            // Whether or not to enable modules dependency checking.
            // Enabled by default, prevents usage of modules that depend on other modules
            // that weren't loaded.
            // 'check_dependencies' => true,
        // Used to create an own service manager. May contain one or more child arrays.
        //'service_listener_options' => array(
        //     array(
        //         'service_manager' => $stringServiceManagerName,
        //         'config_key'      => $stringConfigKey,
        //         'interface'       => $stringOptionalInterface,
        //         'method'          => $stringRequiredMethodName,
        //     ),
        // )
       // Initial configuration with which to seed the ServiceManager.
       // Should be compatible with Zend\ServiceManager\Config.
       // 'service_manager' => array(),
  7. let’s setup config files related to MySqL db connection.. yes you read that right: “files” not “file” 😀
    • first file (the big one): /config/autoload/global.php :
       * Global Configuration Override
       * You can use this file for overriding configuration values from modules, etc.
       * You would place values in here that are agnostic to the environment and not
       * sensitive to security.
       * @NOTE: In practice, this file will typically be INCLUDED in your source
       * control, so do not include passwords or other sensitive information in this
       * file.
      return array(
           'db' => array(
               'driver'         => 'pdo_mysql',
               'dsn'            => 'mysql:host=HOSTNAME;dbname=DBNAME',
               'driver_options' => array(
                   PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'',
                   PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
           'service_manager' => array(
               'factories' => array(
                           => 'Zend\Db\Adapter\AdapterServiceFactory',

      please remember to change correctly HOSTNAME and DBNAME..

    • second file: /config/autoload/local.php :
       * Local Configuration Override
       * This configuration override file is for overriding environment-specific and
       * security-sensitive configuration information. Copy this file without the
       * .dist extension at the end and populate values as needed.
       * @NOTE: This file is ignored from Git by default with the .gitignore included
       * in ZendSkeletonApplication. This is a good practice, as it prevents sensitive
       * credentials from accidentally being committed into version control.
      return array(
           'db' => array(
               'username' => 'USERNAME',
               'password' => 'PASSWORD',

      please remember to change correctly USERNAME and PASSWORD..

To be continued.. 😀

Series Navigation<< DART, AngularDART and Zend Framework 2 [part 1: introduction & db]DART, AngularDART and Zend Framework 2 [part 3: Application module] >>

Leave a Reply

Your email address will not be published. Required fields are marked *