PHP code example of ice-cream / router

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

    

ice-cream / router example snippets


use IceCreamRouter\Router;

$router = new Router();

$router->get('/foo/{id}', 'foo', function($id, $request, $response){
  return response->setContent('Id is: ' . $id);
});

$response = $router->processRoutes(Request::create('/foo/1', 'GET'));

var_dump($response->getContent()); // => Id is: 1

use IceCreamRouter\Router;

$router = new Router();

$router->post('/foo/{id}', 'foo', function($id, $request){
  return new Response('the message passed in was: ' . $request->get('message') . ' and the id is: ' . $id);
});

$response = $router->processRoutes(Request::create('/foo/1', 'POST', ['message' => 'hello world']));

var_dump($response->getContent()); // => the message passed in was: hello world and the id is: 1

use IceCreamRouter\Router;

$router = new Router();

$router->post('/foo/{id}', 'foo', function($id, $request){
  return new Response('the message passed in was: ' . $request->get('message') . ' and the id is: ' . $id);
});

$router->delete('/foo/{id}', 'foo_del', function($id, $request){
  return new Response('deleted');
});

$router->put('/foo/{id}', 'foo_put', function($id, $request){
  return new Response('the message put was: ' . $request->get('message') . ' and the id is: ' . $id);
});

$response = $router->processRoutes(Request::create('/foo/1', 'POST', ['message' => 'hello world']));

var_dump($response->getContent()); // => the message passed in was: hello world and the id is: 1