Cakefest 2020 Workshop 1 and 4

Cakefest 2020 Workshop 1 and 4

Cakefest 2020 Workshops 1 & 4 slides

29fafe10f8a2fefb98671421260aaa65?s=128

Jorge M. González Martín

October 08, 2020
Tweet

Transcript

  1. CakeFest 2020 Virtual Event Workshop 1

  2. Workshop 1 • 4x2h sessions (this is the first one)

    • Starting from scratch • Slides and code • Code - https:/ /github.com/cakephp/cakefest2020/ (check branches) • Database - https:/ /bit.ly/3jB8qeU • Slides - TBD
  3. About Jorge González @steinkelz • CakePHP Developer at CakeDC jorge.gonzalez@cakedc.com

    https:/ /www.cakedc.com • Trainer at Cake Software Foundation https:/ /training.cakephp.org
  4. CakePHP Introduction • Setting up a development environment • Configuration

    • Example application • Baking our application CRUD
  5. Setup dev environment I • Requirements $ sudo apt install

    -y php-cli php-intl php-mbstring php-mysql php-sqlite3 mysql-server zip php-imap php-curl php-zip // setup composer $ composer create-project cakephp/app • Or use any bundle package 5
  6. Setup dev environment II • vagrant box https:/ /www.cakedc.com/jorge_gonzalez/2018/01/17/using-a-vagrant-box-as-quick-environment-for-the-getti ng-started-with-cakephp-training-session

    (old but working, then you'll need to install postgresql) 6
  7. Setup dev environment III • docker / docker-compose • PostgreSQL

    13 as a dependency • PHP stack as a dependency 7
  8. Composer • Create a CakePHP 4 project • https:/ /github.com/cakephp/app

    • composer install • run App\Console\Installer::postInstall $ composer create-project cakephp/app cakefest Installing... … Do you want to remove the existing VCS (.git, .svn..) history? [Y,n]? ←Y $ cd cakefest && cat composer.json {skeleton} 8
  9. Start dev environment • Add configuration for docker-compose.yaml • https:/

    /bit.ly/2SINQ0t • Dependencies defined in code, inside your project and shared with your team $ docker-compose up # list containers running $ docker-compose ps • Navigate to http:/ /localhost:8099/ 9
  10. Configuration • Application class • CakePHP Bootstrap • config/bootstrap.php •

    config/app.php • The environment • We configured our default datasource via environment already! 10
  11. Media server • Bake an admin CRUD • Explore CakePHP

    MVC • Model: Tables & Entities • Controllers • Views
  12. $ curl -L https://bit.ly/3jB8qeU | docker exec -i postgres psql

    -Umy_app • Schema download URL: https:/ /bit.ly/3jB8qeU • Import schema Database schema 12
  13. 13 Database conventions • Plurals • Underscores • _id

  14. $ docker exec -i cakephp /application/bin/cake bake all --everything Bake

    Shell • Build a CRUD for our tables 14
  15. Generated Admin CRUD • Tables and Entities for each database

    table under src/model/{Table | Entity} • Controllers for each CRUD action under src/Controller • Templates for each action under templates/{Controller}/{action} • Note the .php extension for templates 15
  16. • DebugKit • dd() • Debugger • Logger • The

    query log Debug & Log 16
  17. • Table • Associations • Validation • Callbacks • Entity

    • Accessing fields CakePHP model layer 17
  18. • Validation when an Entity is created/modified: “validationDefault()” • Validation

    when an Entity is saved: “buildRules()” • newEntity() vs newEmptyEntity() CakePHP validation 18
  19. • Password length > 8 characters Entity validation 19

  20. • Hash password on user creation Password hashing 20

  21. Authentication • New Plugins • Login • Ajax Login 21

  22. • cakephp/authentication https:/ /github.com/cakephp/authentication • Identification and authentication: Who are

    you? • cakephp/authorization https:/ /github.com/cakephp/authorization • Authorization: Are you allowed? New plugins 22
  23. • New classes and interfaces • Separation of concerns •

    PSR-15 Middleware, it happens before your Controller logic • Store identity in the Request • AuthenticationService cakephp/authentication 23
  24. • Password • Others • Ldap • Token • JwtSubject

    • Callback Identifiers 24
  25. • Session (P) • Form • Others • Cookie (P)

    • HttpBasic (S) • HttpDigest (S) • Token (S) • Jwt (S) Authenticators 25
  26. • AuthenticationMiddleware::process() • AuthenticationService::authenticate() • First valid result wins •

    Persist the identity if not stateless • Redirect based on unauthenticated or login redirect The authentication process 26
  27. • Password based identifiers • Session and Form authenticators Auth

    Configuration 27
  28. • Configure application to use Authentication • Access authentication results

    • Check identity • Fix authentication to use email instead of username • Finish the login action • Bring the user groups via auth finder • Add logout Log the user in 28
  29. • Add login form • Configure ajax request (javascript) •

    Prepare login action to handle application/json Ajax Login 29
  30. Uploading files • New action and form • Simple upload

    using a Behavior 30
  31. • Update the Files/add.php template File upload form 31

  32. • Move the file to a local folder • Configure

    Upload Behavior 32
  33. Questions ?

  34. Thank You ありがとうございました。 Gracias

  35. • Refactor metadata field to use Json type • Using

    db migrations • Working with cells Extra ball 35
  36. Using Migrations Handling your schema changes • Initial migration •

    Baking migrations • Working with migrations
  37. Working with cells Totals • Create stats cell per user

    • Render the cell in the layout • Cache the cell 37
  38. • What’s a cell • When are cells useful Cells

    Intro All-in-one for lightweight processing 38
  39. • Bake a new cell for stats • Implement stats

    logic • Render the cell in the layout • OR use CounterCache StatsCell Total files per user {cell}
  40. • Cache drivers • Groups • Invalidation CakePHP Cache Intro

    & config 40
  41. Working with queues Using a Queue for background processing 41

  42. About Mark Scherer @dereuromark • CakePHP Core Developer • Software

    Architect @ Spryker Systems GmbH
  43. • Response time when triggering unrelated or long running jobs

    • Re-Run for error prone jobs (emails, API) • Scaling and parallelization Queue ‘ Why use deferred execution? 43
  44. • We use https:/ /github.com/dereuromark/cakephp-queue here • For some basic

    demos see https:/ /sandbox.dereuromark.de • More queue plugins see awesome list ( github.com/FriendsOfCake/awesome-cakephp ) Queue Asynchronous background processing 44
  45. Install the plugin (composer) Install the schema/tables $ bin/cake migrations

    migrate -p Queue Queue Setup 45
  46. Check the dashboard: /admin/queue Let’s prepare IDE autocomplete $ bin/cake

    phpstorm generate $ bin/cake code_completion generate Queue Usage 46
  47. Send emails - Contact Form - Register - New message

    Main requirements: - removed from main execution - rerun on errors Queue Basic Usage 47
  48. Upload media - Process the video/image/PDF, create thumbnails - Workflow

    for auto-publish and admin-confirmed one - Notifications to involved parties Main requirements: - Allow workflow to be extended easily - Automate as much as possible Queue Advanced usage 48
  49. Let’s add migrations $ bin/cake bake migration MigrationMedia id title

    type processed published created modified Queue Usage 49
  50. Workshop end Thank you all! 50