PHP code example of codehouse / highway

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

    

codehouse / highway example snippets



$router = new CodeHouse\Highway\Router(__DIR__ . '/src/Controller', 'Controller');
echo $router->serve($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

$builder = new \DI\ContainerBuilder();
$builder->useAnnotations(true);
$router->setDiContainer($builder->build());

class SecurityHandler
{
    public static function admin()
    {
        $auth = new \Services\Authentication();
        if ($auth->getLoggedInUserRole() != UserRoles::ADMIN) {
            throw new InvalidRoleException('Role needed: ' . UserRoles::ADMIN . ', Role found: ' . $auth->getLoggedInUserRole());
        }
    }
}


$router = new CodeHouse\Highway\Router(__DIR__ . '/src/Controller', 'Controller', new SecurityHandler());
echo $router->serve($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

use \CodeHouse\Highway\Annotations as Highway;

/**
 * @Highway\Controller
 */
 class SomeController { .. }
 

use \CodeHouse\Highway\Annotations as Highway;

...

/**
 * @Highway\RequestMapping(value="/users", method="get")
 */
 public function listUsers() { ... }
 
...
 

use \CodeHouse\Highway\Annotations as Highway;

...

/**
 * @Highway\Security(role="admin")
 */
 public function adminOnlyMethod() { ... }
 
...