PHP code example of helvetica / standard

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

    

helvetica / standard example snippets




use Helvetica\Standard\App;
use Helvetica\Standard\Router;
use Helvetica\Standard\Library\Response;

$router = new Router();

$router->set('/hello/<name>', function(Response $response, $name) {
    return $response->withContent('hello ' . $name);
});

(new App)->start();

use Helvetica\Standard\App;
use Helvetica\Standard\Router;
use Helvetica\Standard\Library\Template;
use Helvetica\Standard\Library\Response;

$router = new Router();

$router->set('/hello/<name>', function(Response $response, Template $temp, $name) {
    $output = $temp->render(__DIR__ . '/hello.php', ['name' => $name]);
    return $response->withContent($output);
});

(new App)->start();

use Helvetica\Standard\App;
use Helvetica\Standard\Router;
use Helvetica\Standard\Library\Response;

class PrintController
{
    public function hello(Response $response, $name)
    {
        return $response->withContent('hello ' . $name);
    }
}

$router = new Router();

$router->set('/hello/<name>', [PrintController::class, 'hello']);

(new App)->start();

use Helvetica\Standard\App;
use Helvetica\Standard\Router;
use Helvetica\Standard\Library\Request;
use Helvetica\Standard\Library\Response;
use Helvetica\Standard\Abstracts\ActionFilter;

class SayHelloFilter extends ActionFilter
{
    /**
     * The method hook is injectable, and has a fixed param $next.
     * You can call and return the $next closure to continue
     * or just return a response to terminate the process.
     */
    public function hook(Request $request, $next)
    {
        $params = $this->getParams();
        $name = $params['name'];
        $request->withAttributes(['text' => 'hello ' . $name]);
        return $next();
    }
}

$router = new Router();

$router->set('/hello/<name>', function(Request $request, Response $response, $name) {
    $text = $request->getAttribute('text');
    return $response->withContent($text);
})->setFilters([SayHelloFilter::class]);

(new App)->start();

use Helvetica\Standard\App;
use Helvetica\Standard\Router;
use Helvetica\Standard\Exception\NotFoundException;
use Helvetica\Standard\Abstracts\HttpExceptionHandler;

$router = new Router();

$router->set('/not-found', function() {
    throw new NotFoundException();
});

// create a not found handler
class MyNotFoundHandler extends HttpExceptionHandler
{
    public function getResponse(Response $response)
    {
        return $response->withContent('This is my not found exception message.');
    }
}

$app = new App();

$app->setHandler(App::HANDLE_NOT_FOUND, MyNotFoundHandler::class);

$app->start();

php -S localhost:8080