Slide 1

Slide 1 text

Architecture of a web project on Symfony Maksym Moskvychev

Slide 2

Slide 2 text

Page with a Form in Symfony

Slide 3

Slide 3 text

Validation rules Application design for a page with a form Form Entity Template Symfony 2 Core Controller Request Response Form instance Doctrine If Form is valid It contains Entity Controller asks to save Entity Session Set success message Is Form Valid ? HTTP Request HTTP Response

Slide 4

Slide 4 text

Application design for a page with a form Controller action is responsible for 3 types of requests: 1. Form is rendered first time 2. Handle success form submission 3. There was error in form validation. Render a form with errors. Get form instance Handle Request by the form Render a template Check if Form is Valid Save entity to DB. Add success message to session. Add error message to session. Form is submitted 1 3 2 + + Redirect to some page

Slide 5

Slide 5 text

Page with a form and custom logic Registration service Authentication Service For example, this is a registration form. After user is registered we have to send him welcome email, then notify moderator to check this user, and authenticate. User Mailer Service send welcome email authenticate Doctrine persist in database

Slide 6

Slide 6 text

Page with a form and custom logic Registration service If you have any custom logic – keep it all in a service classes. Get form instance Handle Request by the form Render a template Check if Form is Valid Add error message to session. Form is submitted 1 3 2 + + Redirect to some page

Slide 7

Slide 7 text

Unit testing of the Service class All relations are replaced by mocks Tested class is the only one real class in the test. Only it can be created via new Unit test Registration service Authentication Service User Mailer Service send welcome email authenticate Doctrine persist in database The test itself checks that when user is registered – all needed services will be called.

Slide 8

Slide 8 text

Page with a List in Symfony

Slide 9

Slide 9 text

Application design for a page with a list Template Symfony 2 Core Controller Request Response Doctrine Controller fetch entity repository HTTP Request HTTP Response Entity repository ->findBy() data

Slide 10

Slide 10 text

Introduction of service layer Template Symfony 2 Core Controller Request Response Doctrine Fetch entity repository HTTP Request HTTP Response Entity repository ->findBy() data Service class getListData data

Slide 11

Slide 11 text

Advantages of service layer on a page with a list  Less logic and dependencies in controller.  Ability to re-use data for the list in another place.  Ability to cover fetching logic by Unit test.

Slide 12

Slide 12 text

Build-in abilities of entity repositories findBy( array $criteria, array $orderBy = null, $limit = null, $offset = null )

Slide 13

Slide 13 text

When you need a custom SQL query 1. Create method in entity repository 2. Define your logic in object-oriented way

Slide 14

Slide 14 text

Functional tests in Symfony

Slide 15

Slide 15 text

Tips about Functional tests • Functional tests make requests to controllers, and check response. • Functional tests are good in checking that all components are integrated in a right way. • Amount of tests should by under control.

Slide 16

Slide 16 text

Functional and Unit tests Run Functional tests Clear database Create database schema Run fixtures Run migrations Run test Run test Run Unit tests Run next test Run next test

Slide 17

Slide 17 text

How to set up environment for Functional tests Option 1. Bootstrap file. Option 2. setUp method.

Slide 18

Slide 18 text

Bundles in Symfony

Slide 19

Slide 19 text

Bundle structure Controllers Templates Services Entities Entity Repositories Pictures JavaScript CSS Listeners Presentation layer Service layer Database layer Unit tests Functional tests

Slide 20

Slide 20 text

Use one App Bundle

Slide 21

Slide 21 text

Example of multiple bundle architecture All custom bundles Bundles in Vendors Symfony Framework Bundle Symfony Components Doctrine Twig Core Bundle Site A Bundle Site B Bundle Incorrect case Core Bundle Site A Bundle Site B Bundle

Slide 22

Slide 22 text

Multilingual support in Symfony

Slide 23

Slide 23 text

Purpose of multilingual support Translation of user interface Translation of content Switching between locales  Static text on pages  Form fields  Menus  Messages to user Localized versions of content from DB  How to determine User locale?  Switching between locales Even if your site is only in English now, you should take care of its multilingual abilities. Nobody knows what will be with the project in 5 years. But if the project is success, it will go to another countries.

Slide 24

Slide 24 text

 Use keys instead of strings everywhere  Define real text for each key in translation file. Translation of user interface

Slide 25

Slide 25 text

Validation rules Form Entity Template Symfony 2 Core Controller Request Response Form instance Doctrine If Form is valid It contains Entity Controller asks to save Entity Translation files Session Set success message Is Form Valid ? HTTP Request HTTP Response Translation of user interface

Slide 26

Slide 26 text

Translation of content Custom DB structure. Doctrine ODM

Slide 27

Slide 27 text

Web application security and Symfony

Slide 28

Slide 28 text

Symfony resolves some vulnerabilities Cross Site Scripting SQL Injection Cross Site Request Forgery Information Disclosure Authorization policy

Slide 29

Slide 29 text

Policy: save as it is, escape on output

Slide 30

Slide 30 text

SQL Injection Policy: pass to database raw data, bind variables to query.

Slide 31

Slide 31 text

Cross Site Request Forgery Policy: all not-safe requests should be protected by CSRF token

Slide 32

Slide 32 text

Information Disclosure & Authorization policy Policy: white-list access to resources Security component in Symfony allows to configure: • Firewalls to set up Authentication policy. • User providers to get list of users for Authentication. • Access rules to match a resource and a role. • Role hierarchy to define dependencies between roles. • Encoders to define algorithm to encode passwords.

Slide 33

Slide 33 text

Q & A