PHP code example of terrydjony / routeria

1. Go to this page and download the library: Download terrydjony/routeria 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/ */

    

terrydjony / routeria example snippets




use Symfony\Component\HttpFoundation\Request;
use Routeria\Routeria;

$request = Request::createFromGlobals();
$router = new Routeria;
$router->get('/', function() { echo 'Hello World';});

$router->route($request->getPathInfo(), $request->getMethod());

use Symfony\Component\HttpFoundation\Request;
use Routeria\Routeria;

$request = Request::createFromGlobals();
$router = new Routeria;
$callback = function($fname, $lname) {
  echo "Hello $fname $lname. Nice to meet ya!";
};
$router->get('/greet/{fname:alpha}/{lname:alpha}', $callback);

$router->route($request->getPathInfo(), $request->getMethod());

use Symfony\Component\HttpFoundation\Request;
use Routeria\Routeria;

$request = Request::createFromGlobals();
$router = new Routeria;
$router->get('/', function() { echo 'HTTP METHOD : GET';});
$router->post('/', function() { echo 'HTTP METHOD : POST';});
$router->put('/', function() { echo 'HTTP METHOD : PUT';});
$router->delete('/', function() { echo 'HTTP METHOD : DELETE';});
$router->add('/', function() { echo 'HTTP METHOD : CUSTOM';}, 'CUSTOM');

$router->route($request->getPathInfo(), $request->getMethod());

use Symfony\Component\HttpFoundation\Request;
use Routeria\Routeria;

class User {
  public function getInfo($id, $name) {
    echo 'Hello ' . $name . ' ID: ' . $id;
  }
}

$request = Request::createFromGlobals();
$router = new Routeria;
$router->get('/user/{name:alpha}/{id:int}', 'User::getInfo');
$router->route($request->getPathInfo(), $request->getMethod());

use Symfony\Component\HttpFoundation\Request;
use Routeria\Routeria;

$request = Request::createFromGlobals();
$router = new Routeria;
$router->get('/posts/{title:alpha}', function($title) { echo '<h1>'.$title.'</h1>';})
    ->convert(function($title) {
      return ucwords(str_replace('-', ' ', $title));
    });
$router->route($request->getPathInfo(), $request->getMethod());

use Symfony\Component\HttpFoundation\Request;
use Routeria\Routeria;
use Routeria\RouteCollection;
use Routeria\ControllerRoute;
use Routeria\RouteProviderInterface;

class BlogCollection implements RouteProviderInterface {
  public function register(RouteCollection $collection) {
    $blogRoutes = array(
      'index' => new ControllerRoute('/','Blog::index','GET'),
      'post' => new ControllerRoute('/{id:int}/{title:alnum}','Blog::showPost','GET'),
      'page' => new ControllerRoute('/page/{title:alpha}','Blog::showPage','GET')
      );

    $collection->addRoutes($blogRoutes);
  }
}

$request = Request::createFromGlobals();
$router = new Routeria;

$collection = new BlogCollection;
$router->register($collection);
$router->route($request->getPathInfo(), $request->getMethod());