Book Image

Learning Phalcon PHP

Book Image

Learning Phalcon PHP

Overview of this book

Table of Contents (17 chapters)
Learning Phalcon PHP
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating the module structure


We have already created the basic structure in the previous chapters. The directory structure should look like this:

This is okay. What we need to do here is enable the routing and add some methods to BaseController so that we can move forward. Let's start this process by performing the following steps:

  1. Open the routing.php file from the api module, delete its content, and put in this code:

    <?php
    $versions = [
      'v1' => '/api/v1',
      'v2' => '/api/v2'
    ];
    $router->removeExtraSlashes(true);
    
    // Articles group
    $articles = new \Phalcon\Mvc\Router\Group(array(
      'module' => 'api',
      'controller' => 'articles'
    ));
    
    $articles->setPrefix($versions['v1'].'/articles');
    $articles->addGet('', array(
      'module' => 'api',
      'controller' => 'articles',
      'action' => 'list'
    ));
    
    $router->mount($articles);
  2. Next, we add an array with the available versions of our API, and we tell the router to remove extra slashes. Therefore, a request to /api/v1...