PHP code example of oasis / http

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

    

oasis / http example snippets




use Oasis\Mlib\Http\MicroKernel;
use Symfony\Component\HttpFoundation\Response;

$config = [
    'routing' => [
        'path' => 'routes.yml',
        'namespaces' => ['App\\Controllers\\'],
    ],
];
$isDebug = true;
$kernel = new MicroKernel($config, $isDebug);

$kernel->run();



$config = [
    "routing" => [
        "path" => "routes.yml",
        "namespaces" => [
            "Oasis\\Mlib\\TestControllers\\"
        ],
    ],
];

// initialize MicroKernel with $config



use Oasis\Mlib\Http\MicroKernel;
use Symfony\Component\Routing\Route;

$kernel = new MicroKernel($config, $isDebug);

// inject a single route
$kernel->addRoute('health_check', new Route('/health', [
    '_controller' => 'HealthController::check',
]));

// inject a collection of routes
$routes = new \Symfony\Component\Routing\RouteCollection();
$routes->add('api.users', new Route('/api/users', [
    '_controller' => 'UserController::list',
]));
$kernel->addRoutes($routes);

$kernel->run();



$config = [
    "security" => [
        "firewalls" => [
            "http.auth" => [
                "pattern" => "^/admin/",
                "policies" => [
                    "http" => true
                ],
                "users" => [
                    // raw password is foo
                    'admin' => array('ROLE_ADMIN', '$2y$10$3i9/lVd8UOFIJ6PAMFt8gu3/r5g0qeCJvoSlLCsvMTythye19F77a'),
                ],
            ],
        ],
    ],
];

// initialize MicroKernel with $config



$config = [
    "cors" => [
        [
            'pattern' => '^/cors/.*',
            'origins' => ["my.second.domain.tld"],
        ],
    ],
];

// initialize MicroKernel with $config



/** @var Symfony\Component\Security\Core\User\User $user */
$config = [
    "twig" => [
        "template_dir" => "path to your twig templates",
        "asset_base" => "http://my.domain.tld/assets/",
        "globals" => [
            "user" => $user,
        ], // global variables accessible in twig
    ],
];

// initialize MicroKernel with $config


/** @var Oasis\Mlib\Http\Middlewares\MiddlewareInterface $mid */
$config = [
    "middlewares" => [
        $mid,
    ],
];

// initialize MicroKernel with $config



use Symfony\Component\HttpFoundation\JsonResponse;

class MyViewHandler
{
    function __invoke($rawResult, Symfony\Component\HttpFoundation\Request $request)
    {
        return new JsonResponse($rawResult);
    }
}



/** @var callable $viewHandler*/
$config = [
    "view_handlers" => [
        $viewHandler,
    ],
];

// initialize MicroKernel with $config


class MyErrorHandler
{
    function __invoke(\Exception $e, $code)
    {
        return [
            'message' => $e->getMessage(),
            'code' => $code,
        ];
    }
}



/** @var callable $errorHandler*/
$config = [
    "error_handlers" => [
        $errorHandler,
    ],
];

// initialize MicroKernel with $config



use Symfony\Component\HttpFoundation\Request;

/** @var \Memcached $memcached */
$config = [
    "injected_args" => [
        'cache' => $memcached,
    ],
];

// initialize MicroKernel with $config

class MyController
{
    public function testAction(\Memcached $cache, Request $request)
    {
    }
}