SlideShare ist ein Scribd-Unternehmen logo
1 von 31
The Why Behind PHP
Frameworks
Kirk Madera
Director, Technology @ Robofirm
About Me
• Zend Framemwork 2 Certified
• Magento Certified Plus
• Zend PHP 5.5 Certified Engineer
• Married with two kids
• Metalhead
• Coding experience rooted in C++
• Writing PHP for 10+ years
Objective
Start with a simple HTML site and transform it by solving one problem
at a time into something that resembles a modern day framework.
Overview
• Distribute virtual machines (Virtual Box or VMWare)
• Hands on coding with explanations at each step
• Git tags that map to each step
(Use “git clean -xf; git reset --hard {tagname}” if you get behind)
• Ticket sales site is used as our test site
• Don’t judge my design work
Let’s get started!
1.0: Review plain HTML and discuss issues
• What if you ran a plain HTML site?
• Every artist and show would require a new html file
• A rescheduled show requires multiple file edits
• Duplication causes human error
• Dynamic lists like “Most Popular” maintained in HTML
2.0: Reduce duplication through require statements
• So we need php. Skip to git tag 2.0 to get all files converted to PHP.
• require() for head, header, footer, and before body end
• How to deal with title since it’s dynamic? Global variable?
• Problems with global variables.
• Referring to templates from diff directories (e.g. artists/ vs. /)
• How to deal with links in title?
3.0 Encapsulation – Fixing the global variable problem
• Encapsulation is a big word for containing all logic and data for
something in a single place
• We need classes for this
• We’ll start off simple by making a single class in index.php
3.1 Encapsulation – Apply to shows.php and artists.php
• We’ll apply the same idea to shows.php and artists.php
• What if we had to keep going to all php files in artists and shows?
• And wait a second… duplication is back…
3.2 Encapsulation - Reusing classes
• We need index.php, shows.php, artists.php and all other pages to use
the same TemplateRenderer class
• Start by moving it to it’s own file
4.0 Bootstrapping
• Notice the code at the top of each file is growing
• All pages need a lot of the same things to happen
• Additional examples:
• Set error reporting
• Set a BASE_PATH constant
• Set the cwd() to make working with files easier
• Set an environment based variable like DEVELOPER_MODE
• Create bootstrap.php
• Also add BASE_PATH
4.1 Bootstrapping - Autoloader
• require() needs to be called on every page for TemplateRenderer
• What about when we have more classes?
• Requiring every class would be slow
• Lazy requiring classes with require_once() is also slow
• We can avoid thinking about requires altogether with an autoloader
5.0 Separate Logic from View
• Data/Logic should not live in the view
• Things to separate:
• “Popular” artists/shows list
• All artists/shows lists
• Single artist/show data
• Start by moving data to arrays
5.1 Separate Logic From View - Classes
• Move arrays out of templates and into classes
• We’ll call these Mapper classes since
• We will use static arrays within the Mapper classes
• In the next step, we will make this data dynamic
6.0 Dynamic data
• Work entirely within the Mapper classes for now
• Use built in PDO class
• All mappers need database access.
• Where to put credentials?
• Should I make this connection separately for each mapper?
7.0 Routing and Controllers
• Routing lets us have pretty URLs
• E.g. “/shows/metallica-gexa-pavilion-2016-05-04” instead of
“/shows.php?id=1”
• Front Controller Pattern – All requests go to index.php. Router routes
to the right controller
• Make these URLs functional “/”, “/shows”, “/artists”
• Delete shows.php and artists.php
7.1 Routing and Controllers - Artist and show pages
• Repeat previous step but for view pages
• How to deal with ids in URL?
• We have ugly URLs now… like “artist.php?id=4”
7.2 Routing and Controllers – Pretty URLs
• Change URLs from “artist.php?id=1” to “artist/metallica”
• URL alias mapper
8.0 Move all views into templates – Clean up TemplateRenderer
• Remove the need to specify full template path and file extension
8.1 Move all views into templates
• Move all views into controllers for now
• The old separate PHP top level files are the equivalent of controllers
9.0 App class – Encapsulate bootstrap and run
• This makes it easier to have multiple entry points to your app
• For example, a shell script entry point or an API
10.0 Service Locator - Invokables
• Shh.. This is Dependency Injection
• This is just an easier way to understand it
• We are improving reusability and testability here
10.1 Service Locator - Factories
• Creating dependencies which have their own dependencies
10.2 ReflectionFactory
• Doing it automatically
• Avoid making factories for everything
• Reflection in PHP 7 is fast enough that this is viable
• A production strategy of caching a compiled factory built by reflection
works also. (Magento 2 does something similar)
11.0 File Security – Making a public directory
• All files in the project are currently within the web root
• That means the are web accessible
• Save yourself a security headache and move the document root to a
subdirectory called “public”
• Update Nginx
sudo sed -i
's|/var/www/local.tickets.com|/var/www/local.tickets.com/public|g'
/etc/nginx/sites-available/local.tickets.com;
sudo service nginx restart;
12.0 Config
• Configuration should be separate from code
• Move configuration from public/index.php to config/app.php
• Many strategies could be used for collecting configuration
• This is especially important when you get to more complicated
modular code
13.0 Blocks
• Move logic related to grabbing the data for the view into Block classes
• Thins out the controllers
Compare to Other Frameworks
• Zend Framework 2:
https://github.com/zendframework/ZendSkeletonApplication
• Symfony:
https://github.com/symfony/symfony-standard
• Laravel:
https://github.com/hyfn/laravel-skeleton/tree/master/public
Why use a framework?
• Don’t reinvent the wheel
• Benefit from others’ experience
• Spend time on the core purpose of your site
Resources
• Code:
https://bitbucket.org/robofirm/tickets.com-tutorial
• Site URL:
http://local.tickets.com/
• VM:
https://robofirm.box.com/s/ip0xg5gw2gjomu4nr64col7rjiwnx760
• This Slideshow:
http://www.slideshare.net/KirkMadera/they-why-behind-php-
frameworks

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

NCUG 2019: Spring forward: an introduction to Spring boot and Thymeleaf for (...
NCUG 2019: Spring forward: an introduction to Spring boot and Thymeleaf for (...NCUG 2019: Spring forward: an introduction to Spring boot and Thymeleaf for (...
NCUG 2019: Spring forward: an introduction to Spring boot and Thymeleaf for (...
 
Command box
Command boxCommand box
Command box
 
Agile sites @ telmore
Agile sites @ telmore Agile sites @ telmore
Agile sites @ telmore
 
Agile sites2
Agile sites2Agile sites2
Agile sites2
 
Developing PHP Applications Faster
Developing PHP Applications FasterDeveloping PHP Applications Faster
Developing PHP Applications Faster
 
Getting Started with ASP.net Core 1.0
Getting Started with ASP.net Core 1.0Getting Started with ASP.net Core 1.0
Getting Started with ASP.net Core 1.0
 
Continuous Integration with Open Source Tools - PHPUgFfm 2014-11-20
Continuous Integration with Open Source Tools - PHPUgFfm 2014-11-20Continuous Integration with Open Source Tools - PHPUgFfm 2014-11-20
Continuous Integration with Open Source Tools - PHPUgFfm 2014-11-20
 
Conquering AngularJS Limitations
Conquering AngularJS LimitationsConquering AngularJS Limitations
Conquering AngularJS Limitations
 
RESTFul Tools For Lazy Experts - CFSummit 2016
RESTFul Tools For Lazy Experts - CFSummit 2016RESTFul Tools For Lazy Experts - CFSummit 2016
RESTFul Tools For Lazy Experts - CFSummit 2016
 
Ship It ! with Ruby/ Rails Ecosystem
Ship It ! with Ruby/ Rails EcosystemShip It ! with Ruby/ Rails Ecosystem
Ship It ! with Ruby/ Rails Ecosystem
 
Building Web Apps in Ratpack
Building Web Apps in RatpackBuilding Web Apps in Ratpack
Building Web Apps in Ratpack
 
Instant ColdFusion with Vagrant
Instant ColdFusion with VagrantInstant ColdFusion with Vagrant
Instant ColdFusion with Vagrant
 
An Introduction to the Laravel Framework (AFUP Forum PHP 2014)
An Introduction to the Laravel Framework (AFUP Forum PHP 2014)An Introduction to the Laravel Framework (AFUP Forum PHP 2014)
An Introduction to the Laravel Framework (AFUP Forum PHP 2014)
 
Practical PHP Deployment with Jenkins
Practical PHP Deployment with JenkinsPractical PHP Deployment with Jenkins
Practical PHP Deployment with Jenkins
 
Hidden gems in cf2016
Hidden gems in cf2016Hidden gems in cf2016
Hidden gems in cf2016
 
Keep Applications Online
Keep Applications OnlineKeep Applications Online
Keep Applications Online
 
Asp.Net Core MVC , Razor page , Entity Framework Core
Asp.Net Core MVC , Razor page , Entity Framework CoreAsp.Net Core MVC , Razor page , Entity Framework Core
Asp.Net Core MVC , Razor page , Entity Framework Core
 
Intro to Ratpack (CDJDN 2015-01-22)
Intro to Ratpack (CDJDN 2015-01-22)Intro to Ratpack (CDJDN 2015-01-22)
Intro to Ratpack (CDJDN 2015-01-22)
 
Dev objective2015 lets git together
Dev objective2015 lets git togetherDev objective2015 lets git together
Dev objective2015 lets git together
 
In The Trenches With Tomster, Upgrading Ember.js & Ember Data
In The Trenches With Tomster, Upgrading Ember.js & Ember DataIn The Trenches With Tomster, Upgrading Ember.js & Ember Data
In The Trenches With Tomster, Upgrading Ember.js & Ember Data
 

Andere mochten auch

Flat 4-animated powerpoint jjd
Flat 4-animated powerpoint jjdFlat 4-animated powerpoint jjd
Flat 4-animated powerpoint jjd
haverstockmedia
 
Selenium course syllabus
Selenium course syllabusSelenium course syllabus
Selenium course syllabus
lakshmipriyaaka
 
(Eft)emotional freedom technique cici
(Eft)emotional  freedom technique cici(Eft)emotional  freedom technique cici
(Eft)emotional freedom technique cici
Yan Eshad
 

Andere mochten auch (8)

Flat 4-animated powerpoint jjd
Flat 4-animated powerpoint jjdFlat 4-animated powerpoint jjd
Flat 4-animated powerpoint jjd
 
Kunci dan Perangkat PENJASKES SMP kelas 8
Kunci dan Perangkat PENJASKES SMP kelas 8Kunci dan Perangkat PENJASKES SMP kelas 8
Kunci dan Perangkat PENJASKES SMP kelas 8
 
Selenium course syllabus
Selenium course syllabusSelenium course syllabus
Selenium course syllabus
 
Sesiones de la conferencia
Sesiones de la conferenciaSesiones de la conferencia
Sesiones de la conferencia
 
Reklamos prekybos centruose apžvalga
Reklamos prekybos centruose apžvalgaReklamos prekybos centruose apžvalga
Reklamos prekybos centruose apžvalga
 
Kunci dan Perangkat PENJASKES SMP kelas 9
Kunci dan Perangkat PENJASKES SMP kelas 9Kunci dan Perangkat PENJASKES SMP kelas 9
Kunci dan Perangkat PENJASKES SMP kelas 9
 
(Eft)emotional freedom technique cici
(Eft)emotional  freedom technique cici(Eft)emotional  freedom technique cici
(Eft)emotional freedom technique cici
 
ルイヴィトンのバッグは
ルイヴィトンのバッグはルイヴィトンのバッグは
ルイヴィトンのバッグは
 

Ähnlich wie They why behind php frameworks

Zend Framework 2, What's new, Confoo 2011
Zend Framework 2, What's new, Confoo 2011Zend Framework 2, What's new, Confoo 2011
Zend Framework 2, What's new, Confoo 2011
Bachkoutou Toutou
 
Stress Free Deployment - Confoo 2011
Stress Free Deployment  - Confoo 2011Stress Free Deployment  - Confoo 2011
Stress Free Deployment - Confoo 2011
Bachkoutou Toutou
 

Ähnlich wie They why behind php frameworks (20)

Best Practices for Building WordPress Applications
Best Practices for Building WordPress ApplicationsBest Practices for Building WordPress Applications
Best Practices for Building WordPress Applications
 
Symfony2 for legacy app rejuvenation: the eZ Publish case study
Symfony2 for legacy app rejuvenation: the eZ Publish case studySymfony2 for legacy app rejuvenation: the eZ Publish case study
Symfony2 for legacy app rejuvenation: the eZ Publish case study
 
Codeigniter framework
Codeigniter framework Codeigniter framework
Codeigniter framework
 
Porting Projects to .NET 5
Porting Projects to .NET 5Porting Projects to .NET 5
Porting Projects to .NET 5
 
Code for Startup MVP (Ruby on Rails) Session 1
Code for Startup MVP (Ruby on Rails) Session 1Code for Startup MVP (Ruby on Rails) Session 1
Code for Startup MVP (Ruby on Rails) Session 1
 
Git Going w/ Git
Git Going w/ GitGit Going w/ Git
Git Going w/ Git
 
Codeigniter
CodeigniterCodeigniter
Codeigniter
 
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
 
Continuous Integration at Mollie
Continuous Integration at MollieContinuous Integration at Mollie
Continuous Integration at Mollie
 
Migration from ASP.NET MVC to ASP.NET Core
Migration from ASP.NET MVC to ASP.NET CoreMigration from ASP.NET MVC to ASP.NET Core
Migration from ASP.NET MVC to ASP.NET Core
 
Symfony under control. Continuous Integration and Automated Deployments in Sy...
Symfony under control. Continuous Integration and Automated Deployments in Sy...Symfony under control. Continuous Integration and Automated Deployments in Sy...
Symfony under control. Continuous Integration and Automated Deployments in Sy...
 
Symfony Under Control by Maxim Romanovsky
Symfony Under Control by Maxim RomanovskySymfony Under Control by Maxim Romanovsky
Symfony Under Control by Maxim Romanovsky
 
Sitecore development approach evolution – destination helix
Sitecore development approach evolution – destination helixSitecore development approach evolution – destination helix
Sitecore development approach evolution – destination helix
 
Zend Framework 2, What's new, Confoo 2011
Zend Framework 2, What's new, Confoo 2011Zend Framework 2, What's new, Confoo 2011
Zend Framework 2, What's new, Confoo 2011
 
Writing Services with ZF2
Writing Services with ZF2Writing Services with ZF2
Writing Services with ZF2
 
Stress Free Deployment - Confoo 2011
Stress Free Deployment  - Confoo 2011Stress Free Deployment  - Confoo 2011
Stress Free Deployment - Confoo 2011
 
SOLID Programming with Portable Class Libraries
SOLID Programming with Portable Class LibrariesSOLID Programming with Portable Class Libraries
SOLID Programming with Portable Class Libraries
 
CT Software Developers Meetup: Using Docker and Vagrant Within A GitHub Pull ...
CT Software Developers Meetup: Using Docker and Vagrant Within A GitHub Pull ...CT Software Developers Meetup: Using Docker and Vagrant Within A GitHub Pull ...
CT Software Developers Meetup: Using Docker and Vagrant Within A GitHub Pull ...
 
Headless cms architecture
Headless cms architectureHeadless cms architecture
Headless cms architecture
 
Drupal 8 deeper dive
Drupal 8 deeper diveDrupal 8 deeper dive
Drupal 8 deeper dive
 

Kürzlich hochgeladen

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Kürzlich hochgeladen (20)

Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 

They why behind php frameworks

  • 1. The Why Behind PHP Frameworks Kirk Madera Director, Technology @ Robofirm
  • 2. About Me • Zend Framemwork 2 Certified • Magento Certified Plus • Zend PHP 5.5 Certified Engineer • Married with two kids • Metalhead • Coding experience rooted in C++ • Writing PHP for 10+ years
  • 3. Objective Start with a simple HTML site and transform it by solving one problem at a time into something that resembles a modern day framework.
  • 4. Overview • Distribute virtual machines (Virtual Box or VMWare) • Hands on coding with explanations at each step • Git tags that map to each step (Use “git clean -xf; git reset --hard {tagname}” if you get behind) • Ticket sales site is used as our test site • Don’t judge my design work
  • 6. 1.0: Review plain HTML and discuss issues • What if you ran a plain HTML site? • Every artist and show would require a new html file • A rescheduled show requires multiple file edits • Duplication causes human error • Dynamic lists like “Most Popular” maintained in HTML
  • 7. 2.0: Reduce duplication through require statements • So we need php. Skip to git tag 2.0 to get all files converted to PHP. • require() for head, header, footer, and before body end • How to deal with title since it’s dynamic? Global variable? • Problems with global variables. • Referring to templates from diff directories (e.g. artists/ vs. /) • How to deal with links in title?
  • 8. 3.0 Encapsulation – Fixing the global variable problem • Encapsulation is a big word for containing all logic and data for something in a single place • We need classes for this • We’ll start off simple by making a single class in index.php
  • 9. 3.1 Encapsulation – Apply to shows.php and artists.php • We’ll apply the same idea to shows.php and artists.php • What if we had to keep going to all php files in artists and shows? • And wait a second… duplication is back…
  • 10. 3.2 Encapsulation - Reusing classes • We need index.php, shows.php, artists.php and all other pages to use the same TemplateRenderer class • Start by moving it to it’s own file
  • 11.
  • 12. 4.0 Bootstrapping • Notice the code at the top of each file is growing • All pages need a lot of the same things to happen • Additional examples: • Set error reporting • Set a BASE_PATH constant • Set the cwd() to make working with files easier • Set an environment based variable like DEVELOPER_MODE • Create bootstrap.php • Also add BASE_PATH
  • 13. 4.1 Bootstrapping - Autoloader • require() needs to be called on every page for TemplateRenderer • What about when we have more classes? • Requiring every class would be slow • Lazy requiring classes with require_once() is also slow • We can avoid thinking about requires altogether with an autoloader
  • 14. 5.0 Separate Logic from View • Data/Logic should not live in the view • Things to separate: • “Popular” artists/shows list • All artists/shows lists • Single artist/show data • Start by moving data to arrays
  • 15. 5.1 Separate Logic From View - Classes • Move arrays out of templates and into classes • We’ll call these Mapper classes since • We will use static arrays within the Mapper classes • In the next step, we will make this data dynamic
  • 16. 6.0 Dynamic data • Work entirely within the Mapper classes for now • Use built in PDO class • All mappers need database access. • Where to put credentials? • Should I make this connection separately for each mapper?
  • 17. 7.0 Routing and Controllers • Routing lets us have pretty URLs • E.g. “/shows/metallica-gexa-pavilion-2016-05-04” instead of “/shows.php?id=1” • Front Controller Pattern – All requests go to index.php. Router routes to the right controller • Make these URLs functional “/”, “/shows”, “/artists” • Delete shows.php and artists.php
  • 18. 7.1 Routing and Controllers - Artist and show pages • Repeat previous step but for view pages • How to deal with ids in URL? • We have ugly URLs now… like “artist.php?id=4”
  • 19. 7.2 Routing and Controllers – Pretty URLs • Change URLs from “artist.php?id=1” to “artist/metallica” • URL alias mapper
  • 20. 8.0 Move all views into templates – Clean up TemplateRenderer • Remove the need to specify full template path and file extension
  • 21. 8.1 Move all views into templates • Move all views into controllers for now • The old separate PHP top level files are the equivalent of controllers
  • 22. 9.0 App class – Encapsulate bootstrap and run • This makes it easier to have multiple entry points to your app • For example, a shell script entry point or an API
  • 23. 10.0 Service Locator - Invokables • Shh.. This is Dependency Injection • This is just an easier way to understand it • We are improving reusability and testability here
  • 24. 10.1 Service Locator - Factories • Creating dependencies which have their own dependencies
  • 25. 10.2 ReflectionFactory • Doing it automatically • Avoid making factories for everything • Reflection in PHP 7 is fast enough that this is viable • A production strategy of caching a compiled factory built by reflection works also. (Magento 2 does something similar)
  • 26. 11.0 File Security – Making a public directory • All files in the project are currently within the web root • That means the are web accessible • Save yourself a security headache and move the document root to a subdirectory called “public” • Update Nginx sudo sed -i 's|/var/www/local.tickets.com|/var/www/local.tickets.com/public|g' /etc/nginx/sites-available/local.tickets.com; sudo service nginx restart;
  • 27. 12.0 Config • Configuration should be separate from code • Move configuration from public/index.php to config/app.php • Many strategies could be used for collecting configuration • This is especially important when you get to more complicated modular code
  • 28. 13.0 Blocks • Move logic related to grabbing the data for the view into Block classes • Thins out the controllers
  • 29. Compare to Other Frameworks • Zend Framework 2: https://github.com/zendframework/ZendSkeletonApplication • Symfony: https://github.com/symfony/symfony-standard • Laravel: https://github.com/hyfn/laravel-skeleton/tree/master/public
  • 30. Why use a framework? • Don’t reinvent the wheel • Benefit from others’ experience • Spend time on the core purpose of your site
  • 31. Resources • Code: https://bitbucket.org/robofirm/tickets.com-tutorial • Site URL: http://local.tickets.com/ • VM: https://robofirm.box.com/s/ip0xg5gw2gjomu4nr64col7rjiwnx760 • This Slideshow: http://www.slideshare.net/KirkMadera/they-why-behind-php- frameworks