PHP code example of kminek / url-id

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

    

kminek / url-id example snippets


use Kminek\UrlId\Parser;

$parser = Parser::createWithDefaultProviders();

$urlId = $parser->parse('https://www.youtube.com/watch?v=9Vqyj77tJfo');

/*
Kminek\UrlId\UrlId {#1
  #id: "9Vqyj77tJfo"
  #resource: "video"
  #provider: "Kminek\UrlId\Provider\Youtube"
}
*/

use Kminek\UrlId\Provider\AbstractProvider;

class CustomProvider extends AbstractProvider
{
    /**
     * @return array
     */
    public static function getDomains(): array
    {
        return [
            'sample.com',
        ];
    }

    /**
     * @return array
     */
    public static function getResources(): array
    {
        return [
            UrlIdInterface::RESOURCE_ARTICLE,
        ];
    }

    /**
     * @return string|null
     */
    public function parseArticleResourceId(): ?string
    {
        if (empty($this->path)) {
            return null;
        }

        return $this->path[0];
    }
}

$parser = Parser::createWithDefaultProviders();
$parser->addProvider(CustomProvider::class);

// or
// $parser = new Parser([CustomProvider::class]);

$urlId = $parser->parse('https://sample.com/23');

/*
Kminek\UrlId\UrlId {#1
  #id: "23"
  #resource: "article"
  #provider: "CustomProvider"
}
*/