DART, AngularDART and Zend Framework 2 [part 4: Recipes module]

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

Hopefully this will be last part in PHP before our application will see the lights 😉

  1. in \module\Recipes\config\module.config.php file let’s put something like this:
    <?php
    return array(
         'controllers' => array(
             'invokables' => array(
                 'Recipes\Controller\RecipeInfo' => 'Recipes\Controller\RecipesController',
             ),
         ),
         'router' => array(
             'routes' => array(
                 'recipe' => array(
                     'type'    => 'segment',
                     'options' => array(
                         'route'    => '/recipe[/][:action][/:id][.:format]',
                         'constraints' => array(
                             'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                             'id'     => '[0-9]+',
                         ),
                         'defaults' => array(
                             'controller' => 'Recipes\Controller\RecipeInfo',
                             'action'     => 'index',
                         ),
                     ),
                 ),
             ),
         ),
         'view_manager' => array(
            'template_path_stack' => array(
                'recipe' => __DIR__ . '/../view',
            ),
            'strategies' => array(
               'ViewJsonStrategy',
            ),
         ),
     );
    ?>
    
  2. next file would be \module\Recipes\src\Recipes\Controller\RecipesController.php with content:
    <?php
    namespace Recipes\Controller;
    
    use Zend\Mvc\Controller\AbstractActionController;
    use Zend\View\Model\JsonModel;
    use Recipes\Model\Recipe;
    use Zend\Debug\Debug;
    
    class RecipesController extends AbstractActionController {
        protected $recipesTable;
    
        public function indexAction() {
            $this->layout("layout/plain");
            $recipesData = $this->getRecipesTable()->fetchAll();
    
            return new JsonModel(
                $recipesData
            );
        }
    
        public function getRecipesTable() {
            if (!$this->recipesTable) {
                $sm = $this->getServiceLocator();
                $this->recipesTable = $sm->get('Recipes\Model\RecipeTable');
            }
            return $this->recipesTable;
        }
    }
    ?>
    
  3. now main “bean”: \module\Recipes\src\Recipes\Model\Recipe.php
    <?php
    namespace Recipes\Model;
    
    use Zend\InputFilter\InputFilter;
    use Zend\InputFilter\InputFilterAwareInterface;
    use Zend\InputFilter\InputFilterInterface;
    
    class Recipe {
        public $id;
        public $name;
        public $author;
        public $added;
        public $updated;
    
        protected $inputFilter;
    
        public function exchangeArray($data) {
            $this->id     = (!empty($data['id'])) ? $data['id'] : null;
            $this->name     = (!empty($data['name'])) ? $data['name'] : null;
            $this->author     = (!empty($data['author'])) ? $data['author'] : null;
            $this->added     = (!empty($data['added'])) ? $data['added'] : date('c');
            $this->updated     = (!empty($data['updated'])) ? $data['updated'] : date('c');
        }
    
        public function getArrayCopy() {
            return get_object_vars($this);
        }
    
        public function setInputFilter(InputFilterInterface $inputFilter) {
            throw new \Exception("Not used");
        }
    
        public function getInputFilter() {
            if (!$this->inputFilter) {
                $inputFilter = new InputFilter();
    
                $inputFilter->add(array(
                    'name'     => 'id',
                    'id'     => 'id',
                    'required' => true,
                    'filters'  => array(
                        array('name' => 'Int'),
                    ),
                ));
    
                $inputFilter->add(array(
                    'name'     => 'name',
                    'id'     => 'name',
                    'required' => true,
                    'filters'  => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                    ),
                    'validators' => array(
                        array(
                            'name'    => 'StringLength',
                            'options' => array(
                                'encoding' => 'UTF-8',
                                'min'      => 1,
                            ),
                        ),
                    ),
                ));
    
                $inputFilter->add(array(
                    'name'     => 'author',
                    'id'     => 'author',
                    'required' => true,
                    'filters'  => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                    ),
                    'validators' => array(
                        array(
                            'name'    => 'StringLength',
                            'options' => array(
                                'encoding' => 'UTF-8',
                                'min'      => 1,
                                'max'      => 50,
                            ),
                        ),
                    ),
                ));
    
                $this->inputFilter = $inputFilter;
            }
    
            return $this->inputFilter;
        }
    }
    ?>
    
  4. bare with me for 3 more files, first is \module\Recipes\src\Recipes\Model\RecipeTable.php
    <?php
    namespace Recipes\Model;
    
    use Zend\Db\TableGateway\TableGateway;
    use Zend\Debug\Debug;
    
    class RecipeTable{
         protected $tableGateway;
    
         public function __construct(TableGateway $tableGateway)
         {
             $this->tableGateway = $tableGateway;
         }
    
         public function fetchAll()
         {
             $resultSet = $this->tableGateway->select();
             return $resultSet;
         }
    
         public function countAll() {
            $result = $this->fetchAll();
            return $result->count();
         }
     }
     ?>
    
  5. \module\Recipes\autoload_classmap.php
    <?php
    return array();
    ?>
    
  6. and the last one.. YUPI!! \module\Recipes\Module.php
    <?php
    namespace Recipes;
    
    use Zend\Db\ResultSet\ResultSet;
    use Zend\Db\TableGateway\TableGateway;
    use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
    use Zend\Authentication\Storage;
    use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;
    use Zend\Debug\Debug;
    use Recipes\Model\Recipe;
    use Recipes\Model\RecipeTable;
    
    class Module implements AutoloaderProviderInterface {
    
        public function getAutoloaderConfig() {
            return array(
                'Zend\Loader\ClassMapAutoloader' => array(
                    __DIR__ . '/autoload_classmap.php',
                ),
                'Zend\Loader\StandardAutoloader' => array(
                    'namespaces' => array(
                        __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                    ),
                ),
            );
        }
    
        public function getConfig() {
            return include __DIR__ . '/config/module.config.php';
        }
    
        public function getServiceConfig() {
            return array(
                'factories' => array(
                    'Recipes\Model\RecipeTable' =>  function($sm) {
                        $tableGateway = $sm->get('RecipeTableGateway');
                        $table = new RecipeTable($tableGateway);
                        return $table;
                    },
                    'RecipeTableGateway' => function ($sm) {
                        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                        $resultSetPrototype = new ResultSet();
                        $resultSetPrototype->setArrayObjectPrototype(new Recipe());
                        return new TableGateway('recipe', $dbAdapter, null, $resultSetPrototype);
                    },
                ),
            );
        }
    }
    ?>
    

Now the webservice should be actually running.. next part will be about DART finally 😀

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

Leave a Reply

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