NetinfluenceSecureDisplayBundle is a small simple bundle which protect emails, phone numbers and any text you want from spam bot, by using encryption, ajax and JavaScript.
Before installing this bundle, you need to have a working installation of FOSJsRoutingBundle.
Version 1.x is compatible with Symfony 2 (2.3 - 2.8), version 2.x are compatible with Symfony 2.8, 3.x and 4.x
Code is tested on PHP 5.6 and PHP 7.0 to PHP 7.3
First you need to add netinfluence/secure-display-bundle
to composer.json
:
{
"require": {
"netinfluence/secure-display-bundle": "dev-master"
}
}
note: replace dev-master
with the last version of this bundle.
Register the bundle into the Symfony AppKernel
// app/AppKernel.php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
...
new Netinfluence\SecureDisplayBundle\NetinfluenceSecureDisplayBundle()
);
return $bundles;
}
}
Define the configuration of the bundle into the config file
# app/config/config.yml
netinfluence_secure_display:
# Required, key used to encrypt data
key: "my_super_random_secure_key"
# Optional, you can customize used template here
template: 'NetinfluenceSecureDisplayBundle::secure_display.html.twig'
Register the routing of the bundle to be able to perform ajax requests
# app/config/routing.yml
netinfluence_secure_display:
resource: "@NetinfluenceSecureDisplayBundle/Resources/config/routing.yml"
prefix: /
Publish the assets to be able to use the javascript file
$ php app/console assets:install --symlink web
Add this line in your layout:
<script src="{{ asset('bundles/netinfluencesecuredisplay/js/display.js') }}"></script>
{{ some.data|secureDisplay(label, action, attributes) }}
parameters
- label: optional
- type: string
- value: text to display if the javascript is not enabled
- action: optional
- type: string
- value: action to append before the link (like "tel:012 345 67" or "mailto:john@doe.com")
- attributes: optional
- type: array
- value: html attributes to add to the text
<h4>Here are my personal informations</h4>
{# Default usage #}
<p>My name is : {{ contact.name|secureDisplay }}</p>
{# Custom label when JavaScript is not enabled #}
<p>You can find me at : {{ contact.address|secureDisplay('this address is protected') }}</p>
{# Transform phone number into clicable link #}
{# Can be 'tel', 'mailto', whatever you want #}
<p>My phone number is : {{ contact.phoneNumber|secureDisplay(null, 'tel') }}</p>
{# Custom html attributes #}
<p>My favorite color is : {{ contact.color|secureDisplay(null, null, { 'style': 'color: red' }) }}</p>
Of course, you can mix any of these tree parameters as you want.