This bundle provides a way for voting posts or whatever.
This version of the bundle requires Symfony 4.0+.
If you wish to use default texts provided in this bundle, you have to make sure you have translator enabled in your config.
# config/packages/translation.yaml
framework:
translator: ~
For more information about translations, check Symfony documentation.
Installation is a quick 7 step process (4 if you use Flex):
- Download MsalsasVotingBundle using composer
- Enable the Bundle
- Create your Reference class (post, article or whatever)
- Configure the MsalsasVotingBundle
- Create MsalsasVotingBundle routing
- Update your database schema
- Display the widgets
Require the bundle with composer:
$ composer require msalsas/voting-bundle
Composer will install the bundle to your project's vendor/msalsas/voting-bundle
directory.
Note
If you're using Flex, this is done automatically
Enable the bundle in the kernel:
<?php // app/AppKernel.php
public function registerBundles() {
- $bundles = array(
- // ... new MsalsasVotingBundleMsalsasVotingBundle(), // ...
);
}
The goal of this bundle is to handle votes and clicks for a Reference
(or Post
, Article
or whatever) class and persist them to a database (MySql).
Your first job, then, is to create the Reference
class
for your application. This class can look and act however you want: add any
properties or methods you find useful. This is your Reference
class.
Note
If you're using Flex, this is done automatically
Add the following configuration to your config/packages/msalsas_voting.yaml
file.
.. configuration-block:: .. code-block:: yaml # config/packages/msalsas_voting.yaml msalsas_voting: user_provider: \App\Entity\User # Your ``User`` class negative_reasons: # You can override the default reasons with your translation references - msalsas_voting.negative_reasons.irrelevant - msalsas_voting.negative_reasons.old - msalsas_voting.negative_reasons.tiredness - msalsas_voting.negative_reasons.sensationalist - msalsas_voting.negative_reasons.spam - msalsas_voting.negative_reasons.duplicated - msalsas_voting.negative_reasons.microblogging - msalsas_voting.negative_reasons.erroneous - msalsas_voting.negative_reasons.plagiarism
Note
If you're using Flex, this is done automatically
Now that you have activated and configured the bundle, you have to create the routing file.
You can edit the paths to whatever you want, but including {id}
.
.. configuration-block:: .. code-block:: yaml # config/routes/msalsas_voting.yml positive_vote: path: /vote-positive/{_locale}/{id} controller: Msalsas\VotingBundle\Controller\VoteController:votePositive methods: POST negative_vote: path: /vote-negative/{_locale}/{id} controller: Msalsas\VotingBundle\Controller\VoteController:voteNegative methods: POST
Now that the bundle is configured, the last thing you need to do is update your database schema.
Run the following command.
$ php bin/console doctrine:schema:update --force
For displaying the voting widgets you have to use macros:
{% import "@msalsas_voting/msalsas_voting_widget.html.twig" as msalsas_voting_widget %} # Import macros
{{ msalsas_voting_widget.shakeItCSS() }} # Import CSS macro (optional)
{{ msalsas_voting_widget.shakeItJS() }} # Import JS macro
{{ msalsas_voting_widget.shakeItModal() }} # Import Modal macro (for errors)
<article class="post">
{{ msalsas_voting_widget.shakeIt(post.id) }} # Import the voting widget
<h2>
<a href="{{ path('blog_post', {slug: post.slug}) }}"> # This is just an example
{{ post.title }}
</a>
</h2>
...
{{ msalsas_voting_widget.bottomBar(post.id) }} # Import bottom bar widget (includes negative voting form)
</article>
Also, you have to import Font Awesome if you want to show the bottom bar icons
Instead of using
msalsas_voting_widget.shakeItCSS()
msalsas_voting_widget.shakeItJS()
you can import
vendor/msalsas/voting-bundle/Resources/public/css/msalsas_voting_styles.css
vendor/msalsas/voting-bundle/Resources/public/js/msalsas_voting_shakeIt.js
with your assets.
If you don't use shakeItModal
errors will be displayed with alert
.
Now that you have completed the basic installation and configuration of the MsalsasVotingBundle, you are ready to learn about more advanced features and usages of the bundle.
The following documents are available: