Share
Embed
Cakefest 2020 Workshop 1 and 4
Cakefest 2020 Workshops 1 & 4 slides
Jorge M. González Martín
October 08, 2020
Transcript
50 slides
-
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 -
About Jorge González @steinkelz • CakePHP Developer at CakeDC [email protected]
https:/ /www.cakedc.com • Trainer at Cake Software Foundation https:/ /training.cakephp.org -
CakePHP Introduction • Setting up a development environment • Configuration
• Example application • Baking our application CRUD -
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 -
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 -
Setup dev environment III • docker / docker-compose • PostgreSQL
13 as a dependency • PHP stack as a dependency 7 -
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 -
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 -
Configuration • Application class • CakePHP Bootstrap • config/bootstrap.php •
config/app.php • The environment • We configured our default datasource via environment already! 10 -
Media server • Bake an admin CRUD • Explore CakePHP
MVC • Model: Tables & Entities • Controllers • Views -
$ 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 -
$ docker exec -i cakephp /application/bin/cake bake all --everything Bake
Shell • Build a CRUD for our tables 14 -
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 -
• DebugKit • dd() • Debugger • Logger • The
query log Debug & Log 16 -
• Table • Associations • Validation • Callbacks • Entity
• Accessing fields CakePHP model layer 17 -
• Validation when an Entity is created/modified: “validationDefault()” • Validation
when an Entity is saved: “buildRules()” • newEntity() vs newEmptyEntity() CakePHP validation 18 -
• 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 -
• 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 -
• Password • Others • Ldap • Token • JwtSubject
• Callback Identifiers 24 -
• Session (P) • Form • Others • Cookie (P)
• HttpBasic (S) • HttpDigest (S) • Token (S) • Jwt (S) Authenticators 25 -
• 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 -
• 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 -
• Add login form • Configure ajax request (javascript) •
Prepare login action to handle application/json Ajax Login 29 -
Uploading files • New action and form • Simple upload
using a Behavior 30 -
• Move the file to a local folder • Configure
Upload Behavior 32 -
• Refactor metadata field to use Json type • Using
db migrations • Working with cells Extra ball 35 -
Using Migrations Handling your schema changes • Initial migration •
Baking migrations • Working with migrations -
Working with cells Totals • Create stats cell per user
• Render the cell in the layout • Cache the cell 37 -
• What’s a cell • When are cells useful Cells
Intro All-in-one for lightweight processing 38 -
• Bake a new cell for stats • Implement stats
logic • Render the cell in the layout • OR use CounterCache StatsCell Total files per user {cell} -
About Mark Scherer @dereuromark • CakePHP Core Developer • Software
Architect @ Spryker Systems GmbH -
• 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 -
• 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 -
Install the plugin (composer) Install the schema/tables $ bin/cake migrations
migrate -p Queue Queue Setup 45 -
Check the dashboard: /admin/queue Let’s prepare IDE autocomplete $ bin/cake
phpstorm generate $ bin/cake code_completion generate Queue Usage 46 -
Send emails - Contact Form - Register - New message
Main requirements: - removed from main execution - rerun on errors Queue Basic Usage 47 -
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 -
Let’s add migrations $ bin/cake bake migration MigrationMedia id title
type processed published created modified Queue Usage 49