[x] Create a Product through a form.
[x] A Product is composed of a name, price and a PDF document.
[x] Use semantical configuration in your Bundle so it can be possible to configure the path to the folder upload.
- Generate a bundle called: Vendor/ShopBundle.
- Create a Product entity with 3 properties: name, price and document.
- Add @ORM annotations so we'll be able to persist the product in database.
- Run
php app/console doctrine:database:create
thenphp app/console doctrine:schema:update
. - Create a ProductType class in the "Form" folder with 4 fields: name, price, document and submit.
- Declarate it as a service.
- Create a
ProductController
class extending the Symfony\Bundle\FrameworkBundle\Controller\Controller (so we'll be able to access the container, and some nice methods as shortcut). - Create an action
newAction
in the ProductController with th pattern/product/new
and pass the form to the view. - Restrict HTTP method to access the route (GET and POST)
- Create a view in the
Resources/views/Product
folder callednew.html.twig
rendering the form. - Add constraints on fields with annotations in the Product class.
- Use form theme to surround each field with a fieldset.
- In the template, deactivate the HTML5 validation, and don't forget to add the
form_enctype
. - Call the handleRequest() method in your action and do the logic to handle the submitted values.
- Add logic in the
if ($form->isValid())
statement to persist the product without the upload of document. - Create the
Uploader
service in the folderVendor\ShopBundle\Services
having anupload()
method. - In the upload method, implement the logic to generate the name of your document uploaded, and move the document in the right folder. Return the filename generated.
- Declarate the Uploader as a service and inject the service
filesystem
to it by constructor. - Make a sementical configuration to declarate the upload path.
- Indicate a
vendor_shop.upload_path
in theapp/config/config.yml
with a value"%upload_path%"
. - Declarate the node in the
src/Vendor/ShophBundle/DependencyInjection/Configuration.php
. - Add the argument
upload_path
to the definition of servicevendor.uploader
insrc/Vendor/ShopBundle/DependencyInjection/VendorShopExtension::load()
method. - Don't forget to add the private property
$path
in the uploader service so it can be initialized in the constructor. - In the action, get the uploader service and call the method
upload
. - Don't forget to update the
$product->document
property. - When the product is persisted, redirect the user to the
/product/show/{id}
route to show the product newly added in database. - Create the showAction having the
/product/show/{id}
route pattern. - Create the template show.html.twig in the
src/Vendor/ShopBundle/Resources/views/Product
to display all information of the product.
Et voilà!
Some documentation:
- http://symfony.com/doc/current/cookbook/form/form_customization.html
- http://symfony.com/doc/current/cookbook/bundles/extension.html