PHP code example of ergy / slim-annotations-router

1. Go to this page and download the library: Download ergy/slim-annotations-router library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ergy / slim-annotations-router example snippets


$app = new \Slim\App();
$c = $app->getContainer();
$c['router'] = function() use ($app) {
  return new \Ergy\Slim\Annotations\Router($app,
    '/path_to_controller_files/', // Path to controller files, will be scanned recursively
    '/path_to_cache_files/' // Path to annotations router cache files, must be writeable by web server, if it doesn't exist, router will attempt to create it
  );
};

return new \Ergy\Slim\Annotations\Router($app,
  ['/path_to_controller_files_1/', '/path_to_controller_files_2/', ...],
  '/path_to_cache_files/'
);

// Route pattern (// Route methods (optional), if not set only GET route will be generated
@Route("/home", methods={"GET","POST","PUT"})

// Route name (optional), used to generate route with Slim pathFor() method
@Route("/home", methods={"GET","POST","PUT"}, name="home")

/**
 * File /my/app/controllers/homeController.php
 * @RoutePrefix("/home")
 */
class HomeController
{
  /**
   * @Route("/hello/{name:[\w]+}", methods={"GET","POST"}, name="home.hello")
   */
  public function helloAction($name)
  {
    echo 'Hello '.$name.' !';
  }
}

/**
 * File /my/app/controllers/homeController.php
 * @RoutePrefix("/home")
 */
class HomeController extends Ergy\Slim\Annotations\Controller
{
  /**
   * @Route("/hello/{name:[\w]+}", methods={"GET","POST"}, name="home.hello")
   */
  public function helloAction($name)
  {
    echo 'Hello '.$name.' !';
    
    // Dump the current request details
    var_dump($this->request);
    
    // Dump the current response details
    var_dump($this->response);
  }
}

/**
 * File /my/app/controllers/homeController.php
 * @RoutePrefix("/home")
 */
class HomeController
{
  public function beforeExecuteRoute(Ergy\Slim\Annotations\RouteInfo $route)
  {
    echo 'before ';
  }
  
  public function afterExecuteRoute() // Parameter Ergy\Slim\Annotations\RouteInfo is optional
  {
    echo ' after';
  }
  
  /**
   * @Route("/hello/{name:[\w]+}", methods={"GET","POST"}, name="home.hello")
   */
  public function helloAction($name)
  {
    echo 'Hello '.$name.' !';
  }
}

/**
 * File /my/app/controllers/homeController.php
 * @RoutePrefix("/home")
 */
class HomeController
{
  public function beforeExecuteRoute(Ergy\Slim\Annotations\RouteInfo $route)
  {
    echo 'before ';
  }
  
  public function afterExecuteRoute() // Parameter Ergy\Slim\Annotations\RouteInfo is optional
  {
    echo ' after';
  }
  
  /**
   * @Route("/hello/{name:[\w]+}", methods={"GET","POST"}, name="home.hello")
   */
  public function helloAction($name)
  {
    echo 'Hello '.$name.' !';
    return false;
  }
}