Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cakefest 2020 Workshop 1 and 4

Cakefest 2020 Workshop 1 and 4

Cakefest 2020 Workshops 1 & 4 slides

Jorge M. González Martín

October 08, 2020
Tweet

More Decks by Jorge M. González Martín

Other Decks in Programming

Transcript

  1. 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
  2. About Jorge González @steinkelz • CakePHP Developer at CakeDC [email protected]

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

    • Example application • Baking our application CRUD
  4. 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
  5. Setup dev environment III • docker / docker-compose • PostgreSQL

    13 as a dependency • PHP stack as a dependency 7
  6. 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
  7. 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
  8. Configuration • Application class • CakePHP Bootstrap • config/bootstrap.php •

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

    MVC • Model: Tables & Entities • Controllers • Views
  10. $ 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
  11. 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
  12. • Table • Associations • Validation • Callbacks • Entity

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

    when an Entity is saved: “buildRules()” • newEntity() vs newEmptyEntity() CakePHP validation 18
  14. • 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
  15. • 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
  16. • Session (P) • Form • Others • Cookie (P)

    • HttpBasic (S) • HttpDigest (S) • Token (S) • Jwt (S) Authenticators 25
  17. • 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
  18. • 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
  19. • Add login form • Configure ajax request (javascript) •

    Prepare login action to handle application/json Ajax Login 29
  20. • Refactor metadata field to use Json type • Using

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

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

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

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

    logic • Render the cell in the layout • OR use CounterCache StatsCell Total files per user {cell}
  25. • 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
  26. • 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
  27. Check the dashboard: /admin/queue Let’s prepare IDE autocomplete $ bin/cake

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

    Main requirements: - removed from main execution - rerun on errors Queue Basic Usage 47
  29. 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
  30. Let’s add migrations $ bin/cake bake migration MigrationMedia id title

    type processed published created modified Queue Usage 49