PHP code example of hkt / psr7-asset

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

    

hkt / psr7-asset example snippets



$locator   = new Hkt\Psr7Asset\AssetLocator();
$service   = new Hkt\Psr7Asset\AssetService($locator);
$responder = new Hkt\Psr7Asset\AssetResponder(new Http\Factory\Diactoros\ResponseFactory());
$router    = new Hkt\Psr7Asset\Router();
$assetAction = new Hkt\Psr7Asset\AssetAction($service, $responder, $router);

// ... more code
$assetAction->process($request, $requestHandler)


$route = new \Zend\Expressive\Router\Route('/asset/{vendor}/{package}/{file}', 'Hkt\Psr7Asset\AssetAction', ['GET'], 'hkt/psr7-asset:route');
$route->setOptions([
    'tokens' => [
        'file' => '(.*)'
    ]
]);
$router->addRoute($route);


$router->addRoute(new \Zend\Expressive\Router\Route('/asset/{vendor}/{package}/{file:.*}', 'Hkt\Psr7Asset\AssetAction', ['GET'], 'hkt/psr7-asset:route'));


$this->url('hkt/psr7-asset:route', [
    'vendor' => 'vendor',
    'package' => 'package',
    'file' => '/css/bootstrap.min.css'
]);

$locator->set('vendor/package', '/full/path/to/vendor/package');

$locator->set('vendor/package', '/full/path/to/vendor/package');
$locator->set('vendor/package/css/style.css', '/full/path/to/vendor/package/public/css/style.css');
// override vendor/package style sheet, same applies for js and images
$locator->set('vendor/package/css/style.css', '/full/path/to/application/specific/public/css/style.css');


namespace Vendor\Package;

use Aura\Di\Container;
use Aura\Di\ContainerConfigInterface;

class AppConfig implements ContainerConfigInterface
{
    public function define(Container $di)
    {
        // add one of the http-interop/http-factory library
        $di->set('Interop\Http\Factory\ResponseFactoryInterface', $di->lazyNew('Http\Factory\Diactoros\ResponseFactory'));
    }

    public function modify(Container $di)
    {
        // Map more paths and location as above.
        $assetLocator = $di->get('Hkt\Psr7Asset\AssetLocator');
        // path to exact location
        $assetLocator->set('vendor/package/css/hello.css', '/path/to/web/css/test.css');
        // path to folder
        $assetLocator->set('vendor/package', dirname(dirname(__DIR__)) . '/public');
    }
}