PHP code example of slaxweb / router

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

    

slaxweb / router example snippets



use SlaxWeb\Router\Route;
use SlaxWeb\Router\Factory;
use SlaxWeb\Router\Request;
use SlaxWeb\Config\Factory as Config;
use Symfony\Component\HttpFoundation\Response;

ction (Request $request, Response $response) {
        // ...
        $response->setContent("my content");
    }
);
Factory::container($config)->add($route);

// dispatch the request
$response = Factory::response();
Factory::dispatcher($config)->dispatch(Factory::request(), $response);

// and send response to browser
$response->send()


nit the Pimple Container
$container = new Pimple\Container;

// register services
$container->register(new SlaxWeb\Config\Service\Provider);
$container->register(new SlaxWeb\Logger\Service\Provider);
$container->register(new SlaxWeb\Hooks\Service\Provider);
$container->register(new SlaxWeb\Router\Service\Provider);

// load config, refer to Config component README

// defina a route
$route = $container["router.newRoute"]->set(
    "myUrl",
    Route::METHOD_GET,
    function (Request $request, Response $response) {
        // ...
        $response->setContent("my content");
    }
);
$container["routesContainer.service"]->add($route);

// dispatch the request
$container["routeDispatcher.service"]->dispatch(
    $container["request.service"],
    $container["response.service"]
);

// and send response to browser
$container["response.service"]->send();