Skip to content

Routing

Joshua Parker edited this page Dec 23, 2020 · 9 revisions

Basic Routing

Below is a basic example of setting up a route. The route's first parameter is the uri, and the second parameter is a closure or callback.

/**
 * Step 1: Require autoloader and import a few needed classes.
 */
require('vendor/autoload.php');

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Qubus\Http\Request;
use Qubus\Http\Response;
use Qubus\Injector\Config\Factory;
use Qubus\Injector\Psr11\Container;
use Qubus\Routing\Route\RouteCollector;
use Qubus\Routing\Router;

$container = new Container(Factory::create([
    Container::STANDARD_ALIASES => [
        RequestInterface::class => Request::class,
        ResponseInterface::class => Response::class,
        ResponseFactoryInterface::class => Laminas\Diactoros\ResponseFactory::class
    ]
]));

/**
 * Step 2: Instantiate the Router.
 */
$router = new Router(new RouteCollector(), $container);
//$router->setBasePath('/'); If the router is installed in a directory, then you need to set the base path.

/**
 * Step 3: Include the routes needed
 */
// Prints `Hello world!`.
$router->get('/hello-world/', function () {
    return 'Hello world!';
});

Closure Routing

In passing a closure as a route handler, you need to pass in two arguments: Psr\Http\Message\ServerRequestInterface and Psr\Http\Message\ResponseInterface. Qubus Router requires Laminas/Diactoros. This library includes two classes that implement the two needed interfaces.

/**
 * Step 1: Require autoloader and import a few needed classes.
 */
require('vendor/autoload.php');

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Qubus\Http\Request;
use Qubus\Http\Response;
use Qubus\Http\ServerRequest;
use Qubus\Injector\Config\Factory;
use Qubus\Injector\Psr11\Container;
use Qubus\Routing\Route\RouteCollector;
use Qubus\Routing\Router;

$container = new Container(Factory::create([
    Container::STANDARD_ALIASES => [
        RequestInterface::class => Request::class,
        ResponseInterface::class => Response::class,
        ResponseFactoryInterface::class => Laminas\Diactoros\ResponseFactory::class
    ]
]));

/**
 * Step 2: Instantiate the Router.
 */
$router = new Router(new RouteCollector(), $container);
//$router->setBasePath('/'); If the router is installed in a directory, then you need to set the base path.

/**
 * Step 3: Include the routes needed
 */
// Get hello-world route.
$router->get('/hello-world/', function (ServerRequest $serverRequest, Response $response) {
    $response->getBody()->write('Hello World!');
    return $response;
});

JSON Routing

You may also choose to load routes from a json file by using the loadRoutesFromJson method:

$route->loadRoutesFromJson('routes.json');

Simple JSON Route

{
    "routes": [
        {
            "path": "/users",
            "method": ["GET"],
            "callback": "\\Mvc\\App\\MyControllers\\UsersController@index"
        }
    ]
}

Group JSON Route

{
   "routes":[
      {
         "group":{
            "routes":[
               {
                  "path":"/post/{postId}/comment/{commentId}/",
                  "method":["GET"],
                  "callback":"PostCommentController@show",
                  "name":"post.comment",
                  "namespace": "\\Mvc\\App\\MyControllers"
               },
               {
                  "path":"/post/{postId}/",
                  "method":["GET"],
                  "callback":"PostController@show",
                  "name":"post",
                  "namespace": "\\Mvc\\App\\MyControllers"
               }
            ]
         }
      }
   ]
}

Combined Simple and Group Route

{
   "routes":[
      {
         "group":{
            "routes":[
               {
                  "path":"/post/{postId}/comment/{commentId}/",
                  "method":["GET"],
                  "callback":"PostCommentController@show",
                  "name":"post.comment",
                  "namespace": "\\Mvc\\App\\MyControllers"
               },
               {
                  "path":"/post/{postId}/",
                  "method":["GET"],
                  "callback":"PostController@show",
                  "name":"post.view",
                  "namespace": "\\Mvc\\App\\MyControllers"
               }
            ]
         }
      },
      {
         "path":"/hello-world/",
         "method":["GET"],
         "callback":"TestController@returnHelloWorld",
         "name":"hello.world",
         "namespace": "\\Mvc\\App\\MyControllers"
      }
   ]
}
Clone this wiki locally