brokoskokoli/star-rating-bundle
Install via Composer:
composer require brokoskokoli/star-rating-bundle
(Note: The README mentions blackknight467/star-rating-bundle, but the package is now under brokoskokoli/star-rating-bundle.)
Enable the Bundle:
Add to config/bundles.php (Symfony 4+):
return [
// ...
Brokoskokoli\StarRatingBundle\StarRatingBundle::class => ['all' => true],
];
Configure Twig (Symfony 4+):
Add to config/packages/twig.yaml:
twig:
paths:
'%kernel.project_dir%/vendor/brokoskokoli/star-rating-bundle/Resources/views': BrokoskokoliStarRatingBundle
Include Assets:
Add to your base template (e.g., base.html.twig):
<link rel="stylesheet" href="{{ asset('bundles/starrating/css/rating.css') }}">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="{{ asset('bundles/starrating/js/rating.js') }}"></script>
Render a 4-star rating (e.g., for a product):
{{ render_rating(4, {'readonly': true}) }}
Form Type Setup:
Add the rating form type to your entity (e.g., Product):
use Brokoskokoli\StarRatingBundle\Form\Type\RatingType;
// In ProductType.php
$builder->add('rating', RatingType::class, [
'label' => 'Rate this product',
'required' => false,
'max' => 5,
]);
Template Rendering: Use the form theme or embed the rating directly:
{{ form_row(product.rating) }}
(Ensure form_theme.html.twig extends the bundle’s theme if customization is needed.)
Filter ratings in templates:
{% set averageRating = product.reviews|average_rating %}
{{ render_rating(averageRating, {'max': 5, 'readonly': true}) }}
Handle form submissions via JavaScript:
$('form').on('submit', function(e) {
e.preventDefault();
$.post($(this).attr('action'), $(this).serialize(), function(data) {
// Update UI dynamically (e.g., refresh rating)
$('#rating-display').html(data.rating_html);
});
});
Override default CSS/JS:
vendor/brokoskokoli/star-rating-bundle/Resources/public/css/rating.css to assets/css/.JQuery Dependency:
rating.js.jQuery is not defined errors.Symfony 4+ Configuration:
appKernel.php (Symfony 2/3). Use config/bundles.php for Symfony 4+.BrokoskokoliStarRatingBundle).Asset Paths:
bundles/starrating/) may break if assets are moved. Use asset() or path() helpers:
{{ asset('bundles/starrating/css/rating.css') }}
Form Theme Conflicts:
rating_widget.html.twig is extended properly. Override in templates/BrokoskokoliStarRatingBundle/Form/.Check Console Logs:
Uncaught ReferenceError or TypeError in browser dev tools if ratings don’t render.Validate Form Data:
1, 5). Validate in your controller:
$request->request->get('product_rating', 0); // Default to 0 if invalid
Clear Cache:
php bin/console cache:clear
Custom Templates: Override the default template by copying:
vendor/brokoskokoli/star-rating-bundle/Resources/views/Form/rating_widget.html.twig
to:
templates/BrokoskokoliStarRatingBundle/Form/rating_widget.html.twig
Add Icons:
The bundle uses Font Awesome. Customize icons by modifying the Twig template or overriding CSS classes (e.g., .fa-star-o).
Localization:
Extend the bundle’s translation files (e.g., translations/messages.en.xlf) for custom labels.
How can I help you explore Laravel packages today?