Skip to content

vstelmakh/url-highlight-symfony-bundle

Repository files navigation

Url highlight bundle

Build status Packagist version PHP version License

Symfony bundle for Url highlight library.
Supported Symfony versions: ^2.7, ^3, ^4, ^5.

Installation

Install the latest version with:

$ composer require vstelmakh/url-highlight-symfony-bundle

Setup

If your application is using Symfony Flex - you are ready to go, skip next steps.

Enable the bundle by adding it to the list of registered bundles in the config/bundles.php file of your project:

// config/bundles.php

return [
    // ...
    VStelmakh\UrlHighlightSymfonyBundle\UrlHighlightBundle::class => ['all' => true],
];

Usage

Bundle provide url highlight service which available via autowire or directly from container:

<?php

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use VStelmakh\UrlHighlight\UrlHighlight;

class ExampleController extends AbstractController
{
    public function autowire(UrlHighlight $urlHighlight)
    {
        // do your stuff
    }

    public function fromContainer()
    {
        /** @var UrlHighlight $urlHighlight */
        $urlHighlight = $this->container->get('vstelmakh.url_highlight');
    }
}

Additionally urls_to_html filter available in templates:

{{ 'Basic example http://example.com'|urls_to_html }}

{# output: Basic example <a href="http://example.com">http://example.com</a> #}

More details see usage in Twig url highlight repository.

Configuration

Works out of the box with default configuration:

url_highlight:
    validator: ~
    highlighter: ~
    encoder: ~

If you need to change configuration add desired options to your parameters.yaml or create separate config: config/packages/url_highlight.yaml.
More information about configuration options could be found in Url highlight library.

Example to define validator to not match urls without scheme

  • Define validator service. Use bundled with Url highlight library or create your own implementation of VStelmakh\UrlHighlight\Validator\ValidatorInterface:
# config/services.yaml
services:
    ...
    VStelmakh\UrlHighlight\Validator\Validator:
        arguments: [false]
  • Provide service id in url_highlight.yaml config
# config/packages/url_highlight.yaml
url_highlight:
    validator: VStelmakh\UrlHighlight\Validator\Validator

Credits

Volodymyr Stelmakh
Licensed under the MIT License. See LICENSE for more information.