DART, AngularDART and Zend Framework 2 [part 3: Application module]

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

Previously we only made it to configure db connection.. this time I hope that in the end we will have first web service up and running..
So.. to not waste any more of your time.. grab your keyboard and let magic begin 😉 In case some explanation is needed to anything below -> please ask in comment 😉

  1. in \public\index.php file let’s put something like this:
    <?php
    ini_set("display_errors",true);
    /**
     * This makes our life easier when dealing with paths. Everything is relative
     * to the application root now.
     */
    chdir(dirname(__DIR__));
    
    // Decline static file requests back to the PHP built-in webserver
    if (php_sapi_name() === 'cli-server' && is_file(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))) {
        return false;
    }
    
    // Setup autoloading
    require 'init_autoloader.php';
    
    // Run the application!
    Zend\Mvc\Application::init(require 'config/application.config.php')->run();
    ?>
    

    this is just to run our ZF2 application from browser (starting point)

  2. now let’s move to \module\Application here are couple of files to be created:
    • \module\Application\config\module.config.php:
      <?php
      /**
       * 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
       */
      
      return array(
          'router' => array(
              'routes' => array(
                  'home' => array(
                      'type' => 'Literal',
                      'options' => array(
                          'route'    => '/',
                          'defaults' => array(
                              'controller' => 'Recipes\Controller\Recipe',
                              'action'     => 'index',
                          ),
                      ),
                  ),
              ),
          ),
          'service_manager' => array(
              'abstract_factories' => array(
                  'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
                  'Zend\Log\LoggerAbstractServiceFactory',
              ),
          ),
          'view_manager' => array(
              'display_not_found_reason' => true,
              'display_exceptions'       => true,
              'doctype'                  => 'HTML5',
              'not_found_template'       => 'error/404',
              'exception_template'       => 'error/index',
              'template_map' => array(
                  'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
                  'layout/plain'            => __DIR__ . '/../view/layout/layout_plain.phtml',
                  'error/404'               => __DIR__ . '/../view/error/404.phtml',
                  'error/index'             => __DIR__ . '/../view/error/index.phtml',
              ),
              'template_path_stack' => array(
                  __DIR__ . '/../view'
              ),
              'strategies' => array(
                  'ViewJsonStrategy',
              ),
          ),
      );
      ?>
      

      here we have defined 4 templates

      'template_map' => array(
                  'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
                  'layout/plain'            => __DIR__ . '/../view/layout/layout_plain.phtml',
                  'error/404'               => __DIR__ . '/../view/error/404.phtml',
                  'error/index'             => __DIR__ . '/../view/error/index.phtml',
              ),
      

      and now we should create them:

      • in \module\Application\view\error\404.phtml:
        <h1><?php echo $this->translate('A 404 error occurred') ?></h1>
        <h2><?php echo $this->message ?></h2>
         
        <?php if (isset($this->reason) && $this->reason): ?>
         
        <?php
        $reasonMessage= '';
        switch ($this->reason) {
            case 'error-controller-cannot-dispatch':
                $reasonMessage = $this->translate('The requested controller was unable to dispatch the request.');
                break;
            case 'error-controller-not-found':
                $reasonMessage = $this->translate('The requested controller could not be mapped to an existing controller class.');
                break;
            case 'error-controller-invalid':
                $reasonMessage = $this->translate('The requested controller was not dispatchable.');
                break;
            case 'error-router-no-match':
                $reasonMessage = $this->translate('The requested URL could not be matched by routing.');
                break;
            default:
                $reasonMessage = $this->translate('We cannot determine at this time why a 404 was generated.');
                break;
        }
        ?>
        
        <p><?php echo $reasonMessage ?></p>
        
        <?php endif ?>
        
        <?php if (isset($this->controller) && $this->controller): ?>
        
        <dl>
            <dt><?php echo $this->translate('Controller') ?>:</dt>
            <dd><?php echo $this->escapeHtml($this->controller) ?>
        <?php
        if (isset($this->controller_class)
            && $this->controller_class
            && $this->controller_class != $this->controller
        ) {
            echo '(' . sprintf($this->translate('resolves to %s'), $this->escapeHtml($this->controller_class)) . ')';
        }
        ?>
        </dd>
        </dl>
        
        <?php endif ?>
        
        <?php if (isset($this->display_exceptions) && $this->display_exceptions): ?>
        
        <?php if(isset($this->exception) && $this->exception instanceof Exception): ?>
        <hr/>
        <h2><?php echo $this->translate('Additional information') ?>:</h2>
        <h3><?php echo get_class($this->exception); ?></h3>
        <dl>
            <dt><?php echo $this->translate('File') ?>:</dt>
            <dd>
                <pre class="prettyprint linenums"><?php echo $this->exception->getFile() ?>:<?php echo $this->exception->getLine() ?></pre>
            </dd>
            <dt><?php echo $this->translate('Message') ?>:</dt>
            <dd>
                <pre class="prettyprint linenums"><?php echo $this->exception->getMessage() ?></pre>
            </dd>
            <dt><?php echo $this->translate('Stack trace') ?>:</dt>
            <dd>
                <pre class="prettyprint linenums"><?php echo $this->exception->getTraceAsString() ?></pre>
            </dd>
        </dl>
        <?php
            $e = $this->exception->getPrevious();
            if ($e) :
        ?>
        <hr/>
        <h2><?php echo $this->translate('Previous exceptions') ?>:</h2>
        <ul class="unstyled">
            <?php while($e) : ?>
            <li>
                <h3><?php echo get_class($e); ?></h3>
                <dl>
                    <dt><?php echo $this->translate('File') ?>:</dt>
                    <dd>
                        <pre class="prettyprint linenums"><?php echo $e->getFile() ?>:<?php echo $e->getLine() ?></pre>
                    </dd>
                    <dt><?php echo $this->translate('Message') ?>:</dt>
                    <dd>
                        <pre class="prettyprint linenums"><?php echo $e->getMessage() ?></pre>
                    </dd>
                    <dt><?php echo $this->translate('Stack trace') ?>:</dt>
                    <dd>
                        <pre class="prettyprint linenums"><?php echo $e->getTraceAsString() ?></pre>
                    </dd>
                </dl>
            </li>
            <?php
                $e = $e->getPrevious();
                endwhile;
            ?>
        </ul>
        <?php endif; ?>
        
        <?php else: ?>
        
        <h3><?php echo $this->translate('No Exception available') ?></h3>
        
        <?php endif ?>
        
        <?php endif ?>
      • \module\Application\view\error\index.phtml
        <h1><?php echo $this->translate('An error occurred') ?></h1>
        <h2><?php echo $this->message ?></h2>
        
        <?php if (isset($this->display_exceptions) && $this->display_exceptions): ?>
        
        <?php if(isset($this->exception) && $this->exception instanceof Exception): ?>
        <hr/>
        <h2><?php echo $this->translate('Additional information') ?>:</h2>
        <h3><?php echo get_class($this->exception); ?></h3>
        <dl>
            <dt><?php echo $this->translate('File') ?>:</dt>
            <dd>
                <pre class="prettyprint linenums"><?php echo $this->exception->getFile() ?>:<?php echo $this->exception->getLine() ?></pre>
            </dd>
            <dt><?php echo $this->translate('Message') ?>:</dt>
            <dd>
                <pre class="prettyprint linenums"><?php echo $this->escapeHtml($this->exception->getMessage()) ?></pre>
            </dd>
            <dt><?php echo $this->translate('Stack trace') ?>:</dt>
            <dd>
                <pre class="prettyprint linenums"><?php echo $this->escapeHtml($this->exception->getTraceAsString()) ?></pre>
            </dd>
        </dl>
        <?php
            $e = $this->exception->getPrevious();
            if ($e) :
        ?>
        <hr/>
        <h2><?php echo $this->translate('Previous exceptions') ?>:</h2>
        <ul class="unstyled">
            <?php while($e) : ?>
            <li>
                <h3><?php echo get_class($e); ?></h3>
                <dl>
                    <dt><?php echo $this->translate('File') ?>:</dt>
                    <dd>
                        <pre class="prettyprint linenums"><?php echo $e->getFile() ?>:<?php echo $e->getLine() ?></pre>
                    </dd>
                    <dt><?php echo $this->translate('Message') ?>:</dt>
                    <dd>
                        <pre class="prettyprint linenums"><?php echo $this->escapeHtml($e->getMessage()) ?></pre>
                    </dd>
                    <dt><?php echo $this->translate('Stack trace') ?>:</dt>
                    <dd>
                        <pre class="prettyprint linenums"><?php echo $this->escapeHtml($e->getTraceAsString()) ?></pre>
                    </dd>
                </dl>
            </li>
            <?php
                $e = $e->getPrevious();
                endwhile;
            ?>
        </ul>
        <?php endif; ?>
        
        <?php else: ?>
        
        <h3><?php echo $this->translate('No Exception available') ?></h3>
        
        <?php endif ?>
        
        <?php endif ?>
        
      • \module\Application\view\layout\layout.phtml
        <?php
            echo $this->content;
        ?>
        
      • \module\Application\view\layout\layout_plain.phtml
        <?php
            echo $this->content;
        ?>
        
    • \module\Application\Module.php:
      <?php
      /**
       * 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
       */
       
      namespace Application;
       
      class Module {
      
          public function getConfig() {
              return include __DIR__ . '/config/module.config.php';
          }
       
          public function getAutoloaderConfig() {
              return array(
                  'Zend\Loader\StandardAutoloader' => array(
                      'namespaces' => array(
                          __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                      ),
                  ),
              );
          }
      
      }
       
      ?>
      
Series Navigation<< DART, AngularDART and Zend Framework 2 [part 2: db connection]DART, AngularDART and Zend Framework 2 [part 4: Recipes module] >>

Leave a Reply

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