1. Go to this page and download the library: Download rudak/js-injector 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/ */
rudak / js-injector example snippets
// src/Service/FrontendConfigProvider.php
use Rudak\JsInjector\Harvester\HarvesterInterface;
class FrontendConfigProvider implements HarvesterInterface
{
public function getValues(): array
{
return [
'API_URL' => 'https://api.example.com',
'DEBUG' => $_ENV['APP_ENV'] === 'dev',
'LOCALE' => 'fr_FR',
];
}
}
// src/Controller/AccountController.php
use Rudak\JsInjector\Attribute\JsInjectorDynamic;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
final class AccountController extends AbstractController
{
// Cette action reçoit les données du channel 'user_context'.
#[JsInjectorDynamic('user_context')]
public function account(): Response { /* ... */ }
// Cette action reçoit les données de 'user_context' ET 'feature_flags'.
#[JsInjectorDynamic(['user_context', 'feature_flags'])]
public function dashboard(): Response { /* ... */ }
// Cette page ne reçoit rien de dynamique.
public function home(): Response { /* ... */ }
}
text
Service PHP (provider)
│ implémente HarvesterInterface
│ taggé `rudak.injector`
▼
Collecte (ValuesHarvester) ──▶ validation des noms de variables
▼
Commande `rudak:generate:js`
▼
injection.js (fichier statique, sûr, versionnable)
│ var { API_URL, DEBUG } = INJECTED_VALUES;
▼
votre code JS : console.log(API_URL) // prêt à l'emploi
bash
php bin/console rudak:debug:injection
javascript
import { injectFromDom } from 'js-injector';
const result = injectFromDom('#js-injector-dynamic');
// => { success: true, injected: ['USER_ID', 'CURRENT_LOCALE', 'FEATURE_FLAGS'], errors: [] }
// => null si le tag n'est pas présent dans la page
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.