PHP code example of lordarryn / psrrouter

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

    

lordarryn / psrrouter example snippets


$app->${$method}($path, $callback);

$app->post('/my/path', function($req, $res) {
	//code
	return $res;
});

$app->get('/page/{id}', function($req, $res, $slugs) {
	var_dump($slugs); // array('id' => $id)
	return $res;
});

$app->get('/page/{id}', function($req, $res, $slugs) {
	return $res;
})->regex('/[0-9]{1,3}/');

$app->get('/route/{id}/{author}', function($req, $res, $slugs) {
	return $res;
})->regex('/[0-9]{1,3}/')
  ->regex('/[a-z]{3,15}/');

$app->run($request, $response);


//This is a basic to-do list with PSR interfaces
use PsrRouter\PsrRouter as Router;

$app = new Router();

$app->get('/', function($req, $res) {

	$body = $res->getBody();
	$body->write('<meta http-equiv="content-type" content="text/html; charset=UTF-8">'.PHP_EOL);
	$body->write('<form action="/add" method="post"><input name="add"><input name="post" value="Add something" type="submit"></form>'.PHP_EOL);

	if(!empty($req->getCookieParams()['todolist'])) {
		$values = explode('-', $req->getCookieParams()['todolist']);

		foreach($values as $key => $todo) {
			$body->write('<br /><a href="/del-'.$key.'">X</a> '.$todo.PHP_EOL);
		}
	}

	return $res->withBody($body);

});

//POST request : add a to-do item
$app->post('/add', function($req, $res) {

	if(!empty($req->getParsedBody()['add'])) {
		$add = htmlentities($req->getParsedBody()['add']);
		$values = !empty($req->getCookieParams()['todolist']) ? explode('-', $req->getCookieParams()['todolist']) : array();
		$values[] = $add;
		$cookieHeader = 'todolist='.implode('-', $values);
		return $res->withStatus(302)->withHeader('Location', '/')->withHeader('Set-Cookie', $cookieHeader);
	}

	return $res->withStatus(302)->withHeader('Location', '/');

});

//GET request with URL parameter : delete a to-do item
$app->get('/del-{id}', function($req, $res, $id) {

	if(isset($req->getCookieParams()['todolist'])) {
		$values = explode('-', $req->getCookieParams()['todolist']);

		if(isset($values[$id['id']]))
			unset($values[$id['id']]);

		$cookieHeader = 'todolist='.implode('-', $values);
		return $res->withStatus(302)->withHeader('Location', '/')->withHeader('Set-Cookie', $cookieHeader);
	}

	return $res->withStatus(302)->withHeader('Location', '/');

})->regex('/[0-9]{1,3}/');


//react/http server
use React\EventLoop\Factory,
	React\Http\Server,
	React\Http\Response,
	React\Socket\Server as SocketServer,
	Psr\Http\Message\ServerRequestInterface,
	Psr\Http\Message\ResponseInterface;

	array(
			'Content-Type' => 'text/html',
			'X-Powered-By' => 'PHP '.phpversion()
		)
	);

	//match HTTP request
	return $app->run($request, $response);

});

$socket = new SocketServer(8080, $loop);
$server->listen($socket);

$loop->run();



est = Zend\Diactoros\ServerRequestFactory::fromGlobals(
    $_SERVER,
    $_GET,
    $_POST,
    $_COOKIE,
    $_FILES
);

$response = new Zend\Diactoros\Response('php://memory', 200, [
	'Content-Type' => 'text/html'
]);

$emitter = new Zend\Diactoros\Response\SapiEmitter();

$app = new PsrRouter\PsrRouter();

$app->get('/', function($req, $res) {
	$body = $res->getBody();
	$body->write("Hello world in {$req->getRequestTarget()} \n");
	return $res->withBody($body);

});

$emitter->emit($app->run($request, $response));