PHP code example of alto / importmap

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

    

alto / importmap example snippets


use Alto\ImportMap\ImportMap;

$map = new ImportMap();

// Add a simple mapping
$map->add('react', 'https://esm.sh/[email protected]');
$map->add('react-dom', 'https://esm.sh/[email protected]');

$map = ImportMap::fromFile(__DIR__ . '/importmap.json');

// You can still add entries dynamically
$map->add('app', '/js/app.js');

use Alto\ImportMap\Renderer\HtmlRenderer;

$renderer = new HtmlRenderer();

// Render the import map script and preload links
echo $renderer->render($map);

// Available globally
$map->add('lodash', 'https://unpkg.com/[email protected]/lodash.js');

// Different version for a specific scope
$map->add(
    'lodash', 
    'https://unpkg.com/[email protected]/lodash.js', 
    scope: '/legacy/'
);

$map->add(
    'app', 
    '/js/app.js', 
    integrity: 'sha384-...'
);

$coreMap = new ImportMap(['react' => '...']);
$pluginMap = new ImportMap(['plugin-ui' => '...']);

$coreMap->merge($pluginMap);

use Alto\ImportMap\Resolver\NativeResolver;

$resolver = new NativeResolver();

try {
    $resolution = $resolver->resolve('react', $map);
    // ['url' => 'https://esm.sh/[email protected]', 'integrity' => null]
    
    echo $resolution['url']; 
} catch (ResolutionFailedException $e) {
    // Handle error
}
bash
vendor/bin/php-cs-fixer fix