abdounikarim/lorem-ipsum-bundle
Symfony bundle that generates playful lorem ipsum text. Autowire the KnpUIpsum service to get fake paragraphs, configure options like unicorns and sunshine, and extend the word list by adding your own WordProviderInterface services.
Installation:
composer require knpuniversity/lorem-ipsum-bundle --dev
For non-Flex projects, enable the bundle in AppKernel.php:
$bundles[] = new KnpU\LoremIpsumBundle\KnpULoremIpsumBundle();
First Use Case: Inject the service into a controller or service:
use KnpU\LoremIpsumBundle\KnpUIpsum;
class SomeController extends AbstractController
{
public function index(KnpUIpsum $loremIpsum)
{
$paragraphs = $loremIpsum->getParagraphs(3); // Generate 3 paragraphs
return $this->render('template.html.twig', ['text' => $paragraphs]);
}
}
Where to Look First:
KnpUIpsum class for available methods like getParagraphs(), getWords(), or getSentences().config/packages/knpu_lorem_ipsum.yaml for customization options.Generating Placeholder Content: Use in development to populate templates, forms, or APIs with realistic-looking fake data.
$loremIpsum->getParagraphs(5); // For blog posts or articles
$loremIpsum->getWords(10); // For short descriptions or tags
Integration with Forms: Pre-fill form fields with fake data for testing or demos:
$form->add('description', TextType::class, [
'data' => $loremIpsum->getSentences(2),
]);
Dynamic Content in Twig: Pass the service to Twig templates for client-side rendering:
<div class="placeholder-text">
{{ app.service('knpu_lorem_ipsum.knpu_ipsum').getParagraphs(2) }}
</div>
Command-Line Scripts: Generate bulk fake data for seeding databases or testing:
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class GenerateFakeDataCommand extends Command
{
protected static $defaultName = 'app:generate-fake-data';
public function execute(InputInterface $input, OutputInterface $output): int
{
$loremIpsum = $this->getContainer()->get('knpu_lorem_ipsum.knpu_ipsum');
$output->writeln($loremIpsum->getParagraphs(10));
return Command::SUCCESS;
}
}
Customizing Output:
Override default configuration in config/packages/knpu_lorem_ipsum.yaml:
knpu_lorem_ipsum:
unicorns_are_real: false
min_sunshine: 5
Service Not Found:
knpu_lorem_ipsum.knpu_ipsum is correct.composer.json under require-dev.Configuration Overrides:
knpu_lorem_ipsum.yaml must match the expected keys (unicorns_are_real, min_sunshine). Typos will silently ignore the config.WordProviderInterface Conflicts:
WordProviderInterface implementation is properly registered as a service. Use tags or compiler passes if needed.Performance in Loops:
$cacheKey = 'fake_text_'.md5($count);
$text = $cache->get($cacheKey, function() use ($loremIpsum, $count) {
return $loremIpsum->getParagraphs($count);
});
Check Service Availability: Dump the container to verify the service exists:
dump($this->container->has('knpu_lorem_ipsum.knpu_ipsum'));
Inspect Generated Text: Log the output to debug unexpected results:
$this->logger->debug('Generated text:', ['text' => $loremIpsum->getParagraphs(1)]);
Configuration Validation: Use Symfony’s parameter dumping to verify config:
php bin/console debug:config knpu_lorem_ipsum
Extend Word Lists:
Create a custom WordProvider to inject domain-specific terms:
use KnpU\LoremIpsumBundle\WordProviderInterface;
class CustomWordProvider implements WordProviderInterface
{
public function getWords(): array
{
return ['Laravel', 'Eloquent', 'Blade', 'Artisan', 'Homestead'];
}
}
Register it as a service with the knpu_lorem_ipsum.word_provider tag.
Localization:
Override the default word lists for multilingual support by implementing WordProviderInterface for each language.
Testing: Use the bundle to generate consistent fake data in tests:
public function testSomething()
{
$this->get('knpu_lorem_ipsum.knpu_ipsum')->getParagraphs(1); // Always same output
}
Symfony Flex Compatibility:
If using Flex, ensure the bundle is listed under config/bundles.php:
return [
// ...
KnpU\LoremIpsumBundle\KnpULoremIpsumBundle::class => ['dev' => true],
];
Environment-Specific Config: Use environment variables to toggle features (e.g., disable unicorns in production):
knpu_lorem_ipsum:
unicorns_are_real: '%env(bool:UNICORNS_ENABLED)%'
How can I help you explore Laravel packages today?