title: Phalcon / REST to existing project
body: <p>I developing a phalcon web application which has the following directory structure:</p>
  
  <pre><code>  /app/
          /cache/
              ...
          /config/
              config.php
              loader.php
              services.php
          /controllers/
              contorllerBase.php
              ...
          /models/
              ...
          /views/
              ...
      /public/
          /css/
          /img/
          /js/
          .htacces
          index.php
          webtools.config.php
          webtools.php
      index.html
  </code></pre>
  
  <p>I created this project with phalcon devtools, and its work correctly so far, but now i have to implement a REST function to this project. 
  My questions are:</p>
  
  <ul>
  <li>Where I create to REST logic in this structure?</li>
  <li>What is the recommended way to implement REST ?
  Because i want access views and I need rest too (i.e.: <a href="http://localhost/api/.." rel="nofollow">http://localhost/api/..</a>.)</li>
  </ul>
  
  <p>I found some solution in the internet but they are confusing for me because mostly implement a single REST api without views.</p>
  
  <p>config.php</p>
  
  <pre><code>return new \Phalcon\Config(array(
      'database' =&gt; array(
  ...
      ),
      'application' =&gt; array(
          'controllersDir' =&gt; __DIR__ . '/../../app/controllers/',
          'modelsDir' =&gt; __DIR__ . '/../../app/models/',
          'viewsDir' =&gt; __DIR__ . '/../../app/views/',
          'pluginsDir' =&gt; __DIR__ . '/../../app/plugins/',
          'libraryDir' =&gt; __DIR__ . '/../../app/library/',
          'cacheDir' =&gt; __DIR__ . '/../../app/cache/',
          'logsDir' =&gt; __DIR__ . '/../../app/logs',
          'baseUri' =&gt; '/',
      )
          ));
  </code></pre>
  
  <p>loader.php</p>
  
  <pre><code>&lt;?php
  
  $loader = new \Phalcon\Loader();
  
  /**
   * We're a registering a set of directories taken from the configuration file
   */
  $loader-&gt;registerDirs(
      array(
          $config-&gt;application-&gt;controllersDir,
          $config-&gt;application-&gt;modelsDir
      )
  )-&gt;register();
  </code></pre>
  
  <p>services.php</p>
  
  <pre><code>&lt;?php
  
  use Phalcon\DI\FactoryDefault;
  use Phalcon\Mvc\View;
  use Phalcon\Mvc\Url as UrlResolver;
  use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
  use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
  use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
  use Phalcon\Session\Adapter\Files as SessionAdapter;
  
  /**
   * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
   */
  $di = new FactoryDefault();
  
  /**
   * The URL component is used to generate all kind of urls in the application
   */
  $di-&gt;set('url', function () use ($config) {
      $url = new UrlResolver();
      $url-&gt;setBaseUri($config-&gt;application-&gt;baseUri);
  
      return $url;
  }, true);
  
  /**
   * Setting up the view component
   */
  $di-&gt;set('view', function () use ($config) {
  
      $view = new View();
  
      $view-&gt;setViewsDir($config-&gt;application-&gt;viewsDir);
  
      $view-&gt;registerEngines(array(
          '.volt' =&gt; function ($view, $di) use ($config) {
  
              $volt = new VoltEngine($view, $di);
  
              $volt-&gt;setOptions(array(
                  'compiledPath' =&gt; $config-&gt;application-&gt;cacheDir,
                  'compiledSeparator' =&gt; '_'
              ));
  
              return $volt;
          },
                  '.phtml' =&gt; 'Phalcon\Mvc\View\Engine\Php'
              ));
  
              return $view;
          }, true);
  
          /**
           * Database connection is created based in the parameters defined in the configuration file
           */
          $di-&gt;set('db', function () use ($config) {
              return new DbAdapter(array(
                  'host' =&gt; $config-&gt;database-&gt;host,
                  'username' =&gt; $config-&gt;database-&gt;username,
                  'password' =&gt; $config-&gt;database-&gt;password,
                  'dbname' =&gt; $config-&gt;database-&gt;dbname,
                  "charset" =&gt; $config-&gt;database-&gt;charset
              ));
          });
  
          /**
           * If the configuration specify the use of metadata adapter use it or use memory otherwise
           */
          $di-&gt;set('modelsMetadata', function () {
              return new MetaDataAdapter();
          });
  
          /**
           * Start the session the first time some component request the session service
           */
          $di-&gt;set('session', function () {
              $session = new SessionAdapter();
              $session-&gt;start();
  
              return $session;
          }); 
  </code></pre>
  
  <p>index.php</p>
  
  <pre><code>&lt;?php
  error_reporting(E_ALL);
  
  $debug = new \Phalcon\Debug();
  $debug-&gt;listen();
  
  try {
      /**
       * Read the configuration
       */
      $config = include __DIR__ . "/../app/config/config.php";
  
      /**
       * Read auto-loader
       */
      include __DIR__ . "/../app/config/loader.php";
  
      /**
       * Read services
       */
      include __DIR__ . "/../app/config/services.php";
  
      /**
       * Handle the request
       */
      $application = new \Phalcon\Mvc\Application($di);
  
      echo $application-&gt;handle()-&gt;getContent();
  } catch (\Exception $e) {
      echo $e-&gt;getMessage();
  }
  </code></pre>
  
  <p>I really apreciate any kind of help.</p>