2. Who are we?
Thailand PHP User Group - A group for bringing
together PHP users in Thailand
HotelQuickly - The Number 1 last minute booking app
in Asia Pacific
Alex Stansfield - Software Development Manager at
Gomeeki, 12 years PHP developer
3. Topics for the evening
What is a Framework?
Why would I want to use one?
What kind of Frameworks are there?
Full Stack Top Three (Laravel, Symfony, Nette)
Micro Frameworks (Silex, Slim3, Lumen)
Benchmarks
HQ Talk: Writing Framework Independent Code
Questions and Discussion
4. What is a Framework?
“Standard Solution to Common problems”
All the code you used to write or pull from
your other projects packaged up for you
Provides a structure to your code
Community
5. Why would you want to use one?
Saves you from reinventing the wheel
Community Resources (plugins, tutorials)
Open Source
Some of the best minds involved
Many eyes to review the code
Contribute
Keep code consistent between developers
6. What kind of frameworks are there?
Frameworks mostly fall into three groups:
Micro Frameworks
Full Stack Frameworks
Content Management Frameworks
7. What is a Micro Framework?
Minimal frameworks
giving you a fast shell
for your code
Dependency Injection
Container
Routing
Middleware Support
Request and Response
Handling
Basic Error Handling
9. Request
Everything from the Request wrapped up in
easy to access Object.
$_POST[‘this’], $_GET[‘that’], $_SERVER[‘REQUEST_METHOD’]
$request->has(‘this’);
$request->get(‘that’);
$request->getMethod();
$request->getHeader(‘Content-Type’);
11. Example
$app->get('/hello[/{name}]', function ($request, $response) {
$response->write("Hello, " . $request->get('name'));
return $response;
})->setArgument('name', 'World!');
GET /hello
Hello, World!
GET /hello/alex
Hello, alex
12. What is a Full Stack Framework?
Contains many
components to help
you rapidly develop
large applications
DBAL and ORM
Multiple configuration files
Caching
Logging
Detailed Error Handling
Console Commands
Events
Templating Engine
Forms and validation
Security
13. Full Stack Top Three
Sitepoint Survey into PHP Framework
popularity from March 2015
14.
15. The Full Stack Big Three
Sitepoint Survey into PHP Framework
popularity from March 2015
1.Laravel
2.Symfony2
3.Nette
16. Laravel
Aiming to make web development easier
Builds upon third party components
Facade provides easy access to services
More components than other frameworks
Homestead Vagrant box
Suited to all sizes of project
17.
18. Illuminate
Illuminate wraps a number of 3rd party
libraries and framework components
Provides more features (Queues, Cloud)
Provides more choice with Contracts
Fairly tight coupling
19. Facades
Static interface to classes that are in service
container
Facades for existing Illuminate services
Can create your own facades to access your
services
Seen by some as promoting bad practise
Injecting Services is still available
20.
21.
22. Laravel - Round Up
Large community and user base
Very easy to pick up and configure
Illuminate provides more out of the box
Homestead helps you get developing
Documentation focuses more on Facade
Good for projects of any size
Quite Tightly coupled so harder to leave
23. Symfony2
Heavy Inspiration from Ruby on Rails
Building Robust Enterprise Applications
Highly Customisable
Powerful Profiling and Debug tools
Includes popular 3rd party components
Many useful Bundles available
Learning curve can be steep
26. Third Party Bundles
User Registration, Login and Profile
OAuth Interface to many social logins
Automatic Rest Handling
Web Service Api Documentation
Image Manipulation
Easy Access to Third Party Libraries
27.
28. Symfony2 - Round Up
Large community and user base
Powerful Profiling and Debugging Tools
Loosely Coupled and 3rd party Components
Very configurable (a pro and a con)
Can be daunting to new users and steeper
learning curve
Better for larger projects
29. Nette
Mature but little known outside Czech
Designed around MVP over MVC
Tracy debugger and profiler
Detailed Error Reporting
Latte templating component
Quite loosely coupled components
35. Latte - Templating Engine
Template Inheritance
Context aware Escaping
Macros inline in HTML tags (like AngularJS)
Filters for content manipulation
User defined Filters and Macros
Compiles templates to PHP so very fast
No dependencies on other libraries
38. Nette - Round Up
Easy learning curve
Useful debugger and profiler
Great Error reporting in dev mode
Fantastic Templating Engine
Small community outside Czech Republic
Placement on Sitepoint survey will bring it
more attention
40. Micro Frameworks
Very minimal install
No path structure (most of the time)
Much faster than Full Stack Frameworks
Ideal for small projects, quick micro services
or command line applications
41. Silex
Closely related to Symfony2
Uses Pimple v1 and Symfony2 Components
Wide range of Official and Third Party Service
Providers
Version 2 under development
42.
43. Slim 3
Still in Beta
Follow up to the very fast Slim 2
Embraces Standards
container-interop interface
PSR-7 Requests and Responses
Routing with Nickc’s FastRoute
Aimed at Web Applications
46. Lumen
From the Laravel Team
Provides a structure for your project
Includes Artisan helper command
Console command support out of the box
Embraces Laravel’s aim of being easy to learn
and use
Support for PSR-7 requests and responses
47. Benchmarks
Setup:
Default install of the framework
Using recommended way to output text
Prints “Hello World!”
Run on PHP 5.6 and 7 beta
ApacheBench over 10 seconds
Don’t hate me, just for guidance
56. Domain-Driven Design
“Domain-driven design is not a
technology or a methodology. DDD
provides a structure of practices and
terminology for making design
decisions that focus and accelerate
software projects.”
Eric Evans
57. Hexagonal Architecture
“Allow an application to equally be driven by users,
programs, automated test or batch scripts, and to be
developed and tested in isolation from its eventual run-time
devices and databases”
- Alistair Cockburn
59. High-level modules should not depend on low-level modules. Both should
depend on abstractions.
A code is worth a thousand words!
Dependency Inversion Principle
Bring together PHP users in Thailand to discuss and learn about PHP
Had a few meetups before but hope to make them more regular
Look for us on Facebook
Topics….
Questions for audience
Who here hasn't used a PHP Framework before?
So hands up who hasn't used one of the ones I've mentioned in the previous slide?
What are you using?
Let's look at it this way.
Long term php and non frameworks
Those repetitive pieces of code you always write when you start a project.
Overtime I'm sure many of you started collecting these into a personal library that you could bring out each time you start a project.
All the code
Ties together those useful bits of code
Structure
Provides a structure to your code
Not just where to put files and what to call them
But generally also an MVC layer that brings together your Models, Views and Controllers
Community
Group of people all using that framework
Some big some small
But generally you don’t feel alone
Save Time
Save yourself time
Don’t need to constantly reinvent the wheel
Other people already done the work
Not just the framework and it’s components that save you time
Community Resources
Community surrounding it brings plugins, tutorials, and help
Most big frameworks have official or recognised tutorial sites
Lots of other sites with tutorials and videos
Lots of people on Stack overflow ready to answer
Open Source
Every Major Framework is an Open Source project
Great minds working together to solve common goals
Many eyes to review the code
Contribute features and fixes
Teams
Guide people to code in similar ways
Keeps code consistent
Helps with code reviews
Content Management Frameworks are things like Drupal and Wordpress
We won’t be focusing on them
Micro Frameworks
generally very minimal.
Give you a lot of freedom
Not many features out of the box
nice quick way to get routing, request and response handling code running
allowing you to focus on your logical code.
Features….
Best Applications
need to run really fast
are very small
command line applications
Suppose you have these two paths to your site
In the past you might have a php file structure like this
With routing you can have a structure like this
Everything through a single entry point
Routing then decides what code needs to be executed depending on the path
Take this path
With this route you can now match the path easily without worrying about the content of each part
Then you can populate the request automatically
Take this code example
See the route is defined here
Inside the handling code it adds some text to the response
Can anyone tell me what this would output?
And how about this?
Full Stack frameworks
packed with features and components that you'll need when developing larger applications
Things you can expect to find in a typical framework:
Database Abstraction Layer and ORM
Support for Multiple configuration files
allowing you to configure the application different for your dev, staging production
Caching
Logging
Not just writing to a file
support writing to No SQL or a service like loggly
Detailed Error Handling
Console Commands
Events
Templating Engine - for building your views and pages.
Form building and validation
Security
Some frameworks are built entirely from scratch. Some use Third Party libraries either directly or wrapped in their own component and other will use a combination.
Top 3
Interesting result as I'd never actually heard of Nette before this.
I'll go into how they managed to sneak into third place later.
Good to pick these three to talk about tonight.
Focus
Mostly offer the same features
Focus on their strong points and the things that set them apart
Also the documentation and community around them.
Aim
Try and be as objective about them as possible.
At the end I'll ask you to guess which frameworks I choose myself and we'll see how well I did.
Familiarity might make mistakes
Happy to be correct
Try to save questions and comment
But put your hand up if you want to say something
So let's start with the first place. Laravel.
Billed as easy to use framework
Takes the pain out of web development by making common tasks easier
Builds on 3rd Party
It builds upon a number of third party components including from symfony2.
Facade
It provides a "Facade" that gives static proxies to services, allowing you to avoiding dealing with dependencies.
More Components
It provides more components than the other frameworks.
Homestead
Provides a great vagrant box setup
Everything you need for php development
Be up and running in no time.
All sizes
Used for enterprise level applications, or simple JSON APIs
Perfectly suited to all types and sizes of projects.
Focus
Focus this evening on
The component library
Facades
Framework is built entirely from it's own set of components known as Illuminate.
Some of these components wrap up and extend a number of 3rd party components.
More Features
More features than most frameworks.
Some examples:
Support for message queues out of the box
Cloud storage.
Can be added to a project in another framework. it does make it easier when it's already there.
Contracts
Uses something it calls contracts.
Provide a common interface to multiple libraries that perform same function
For example the Mailer contract defaults to the swiftmailer library for sending mail via SMTP
However, it also has drivers for Mailgun, Mandrill and Amazon SES.
To switch your mail sending from swiftmailer to one of these others you simply change the configuration.
Fairly tight coupling
These aspects of the Illuminate design make building fully featured Laravel projects very easy.
Downside to this is that are more tightly coupled
Harder, although not impossible, to use them outside of Laravel.
Trade off between easy to learn and quick to write projects, but having a lot more work in the future
A popular, if controversial, feature of laravel is the facades.
Facade provide static proxies to object instances that are in service container.
Avoid dependency injection to access your services
call them with static methods from anywhere in your code.
Existing Services
Laravel ships with Facades for existing Illuminate services
Create your own
you can create your own facades to access the services you create.
Bad Practise
This does cause some controversy as it is seen by some as promoting bad practices
harder to unit test
Injecting
As I’ll show now Injecting Services is still available
Created RequestThingy class
Single method to return if the “version” parameter exists in the request
Unable to unit test
Using dependency injection can inject request object
Call method on object
Can unit test
Documentation
Official documentation is very good
Well laid out and easy to follow
Very focused on using Facade might be difficult for users of other Frameworks
With some digging can find the way to do things how you want
Could be clearer in the documentation how to avoid Facades.
However for beginners the documentation is comprehensive and easy to follow.
Each component is clearly documented.
Community
Very large community
Laracasts is a great site for getting help and discussion.
Contains many tutorials.
Not all the content is free and there is a subscription payment available.
There is a large amount of support on Stack Overflow. I'll show some figures from that later.
Has a Large community and user base
Very easy to pick up and configure
Illuminate provides more out of the box than other frameworks
Homestead helps you get developing quickly and easily
Documentation focuses more on Facade which might not be everyone's cup of tea
Good for projects of any size
Illuminate is quite tightly coupled so harder to leave or to use outside of Laravel
Our second place in the SitePoint survey is Symfony2.
Inspiration
Taken heavy inspiration from the likes of Ruby on Rails
Aimed at
Aimed at building robust enterprise level applications.
Customisable
Gives developers full control over the configuration:
from the directory structure to the third party libraries, almost everything can be customized.
Can configure it in php, yml, xml or annotated comments.
Profiling and Debugging
Bundled with powerful tools to help developers test and debug.
Enabled by default in you development environment.
Components
Include popular 3rd party libraries such as Doctrine, Swiftmailer and Twig
Also used in other Frameworks.
These third party libraries are not wrapped up in a symfony component unlike Laravel
Bundles
Good library of useful Bundles, essentially plugins, available
Help you add powerful functionality to applications very quickly and easily.
Learning Curve
Focuses heavily on using dependency injection to setup your services. T
Learning curve for Symfony can be a bit steep if you're new to frameworks or dependency injection
Initial Config file is large
Talk Focus
Profiling and Debugging tools.
Examples of useful Bundles
Maturity of the Components.
Other than the useful link to help you get started you may notice this bar on the bottom.
This is the debug toolbar and is part of the powerful tools I mentioned a moment ago.
In development mode this toolbar is on by default, though can be disabled.
Live Demo
Basic statistics about the page:
Time taken
Memory used
Queries performed
Click through for more details
Queries Performed
Request Detail
Forms submitted
Emails Sent
Errors
Log
Timeline
Incredible useful when developing large and complex applications.
Another strong part of Symfony are the Third Party Bundles.
FOS User Bundle
Provides user registration, login, roles and profiles out of the box.
Can add an entire customisable user layer to your application in minutes.
OAuth Bundle
Add login to your site through over 40 social and other logins.
Combining this with the above can having you registering and logging in users through the likes of Facebook and Google in a less than an hour.
Rest APIs
Rest handling that will automatically translate json or xml requests to request parameters
Automatically convert your data responses into the format the client is asking for
Web API Documentation
Automatic Web API documentation based on doc block annotations
Builds site that you can put online and let people use to interface with your API
Image manipulation
Creating thumbnails
Doing transformations and edits on images
Other Bundles
Many more bundles for popular third party libraries
Integrate them into symfony giving you easy configuration
Setup the services in the container for you
Documentation
Official documentation is pretty comprehensive.
The main documentation takes you through each of the components and how they work together.
Also provide what they call the Cookbook
Cookbook is a growing collection of recipes for solving the most common problems that developers come across.
Recipes cover so many useful topics - google results point to them
Community
Quite European centric
A lot more likely to meet other symfony developers at a meetup like this in European countries.
However the community is large enough that there is plenty of help to be found online.
Stack overflow has many questions and answer
Youtube has many videos tutorials
Also KNP University which, like laracasts, provides an incredible amount of video tutorials, although many you need to pay for.
Like Laravel it has a Large community and user base
Very Powerful Profiling and Debugging Tools
Mature Loosely Coupled and 3rd party Components
Very configurable which can be both a blessing and a curse
Can be daunting to new users and steeper learning curve
Better for larger projects
Mature
Nette managed to secure an interesting 3rd place
Relatively unheard of outsize Czech Republic
25% of vote in survey from country with 10 million people
MVP
Rather than calling itself an MVC framework it embraces MVP instead.
That's Model-View-Presenter.
MVC is generally quite loosely applied in PHP circles
So most people won’t will really notice much difference between MVC and MVP.
Tracy
Includes it's own custom built debugger and profiler called Tracy
Helping you to solve any development issues.
Error Reporting
Detailed Error Reporting
Lets you drill down into errors when they occur in development
Helps you to find the root of a problem easily.
Latte
Powerful templating engine called Latte
Many useful features
Can be easily be used independently of the framework itself.
Components
Overall a good range of custom components that are quite loosely coupled
Like Symfony 2 Nette comes with a Debugger and Profiler.
Live Demo
Not as feature complete as Symfony's but provides useful tools for the developer to see how well his application is working.
The Error handling is very good and lets you drill down and see exactly what was going on with your application when it went wrong.
The Latte Templating Engine has some pretty nice features to make building your templates easier.
As you'd expect with a templating engine it features template inheritance letting you build up you page in layers
Context aware escaping of strings.
It can tell if you're inserting a string into HTML or Javascript and it will escape it appropriately
As you can see from this example.
Macros
like most templating engines you can write your macros around some HTML
Inline Macros
also supports macros inline in HTML
As you can see from this example
Also notice the ul element that depends on $items existing
Filters
Provides Filters for content manipulation
This example it will capitalize the item string
Many built in filters allowing you to strip, pad, trim, change case, format dates and numbers.
Custom Filters and Macros
The built in filters and macros can be supplemented by user defined ones you can write yourself in PHP.
Compiles it's templates into PHP which it caches on disk.
When adding the PHP OpCache this feature helps make latte very fast.
Lastly it has no dependencies on other libraries.
Latte
Composer.json requirements for the Latte library
No other php libraries
Blade
Composer.json requirements for Blade
Requires 5 illuminate packages
In total 9 packages installed compared to 1 for Latte
The official documentation is available in Czech and English.
Quite comprehensive and easy to follow unfortunately it's only available in those two languages.
Documentation is very good and explains the framework and components clearly
BUT once you get away from official documentation the lack of international exposure to Nette becomes somewhat more apparent.
This also means the community surrounding it is very much based in the Czech Republic.
Hopefully this will soon change.
Easy learning curve (if you speak English or Czech)
Useful debugger and profiler
Great Error reporting in dev mode
Fantastic templating engine
can be easily used independently of Nette
Unfortunately it has a fairly small community outside Czech Republic
Placement on Sitepoint survey will bring it more attention
You should expect to have to put in more of your own time to solve problems not addressed in the documentation.
Symfony overall has the most Questions
Looking at the recent asks laravel is really in the lead
Nette shows it’s lack of international attraction
Symfony 2
Closely related to Symfony2
Components
Uses version 1 of the popular Pimple depenency injection container.
Makes use of Symfony2 components for other parts
Service Providers
Service providers with act a bit like Bundles in Symfony2.
Decent number of Official and Third Party Service providers that bring support for things such as
Doctrine
Twig
Message Queues
Amazon Webservice
console command support
and a number of other useful things.
Version 2
Version 2 is under development and will use Version 3 of Pimple
From this recent commit
Release date for version 2 is at best fluid
Beta
Still in beta at the moment.
Beta 2 was released just last week and we should see stable release soon.
Slim 2
Brought to you by the guys behind the speedy Slim 2.
Standards
Built with standards in mind.
Container
Defaults to Pimple version 3
Supports any container that conforms to the container-interop Interface.
Allows you to swap to a container you might be more familiar with
PSR-7
It's Request and Response classes are implementations of the new PSR-7 standard of HTTP message interfaces.
Code you write to use (middleware/controllers) can be use in other frameworks that support PSR-7
Already Symfony2 and Laravel 5.1 allow for bridging their own requests and responses into PSR-7.
Routing
For routing it uses the incredibly quick FastRoutes library by Nikita Popov.
Web focus
Aimed at Web Applications and no real console support that I’ve seen
Brought to you by the Laravel Team
Provides a structure for your project.
Composer require for slim or silex in a new directory
After running the Lumen installer you will have this instead.
More welcoming
Artisan
Include Artisan console command
helps you to perform a number of common tasks easily.
Console Commands
Has console command support out of the box
Very useful in a micro framework as command line applications are a good use of them.
It embraces Laravel’s aim of being easy to learn and use
Like Slim 3 it has support for PSR-7 requests and responses
Kenji Suzuki’s existing php framework benchmark repository
Added Slim 3, Nette and Lumen 5.1 benchmarks
Used Ramsus php7dev vagrant virtual machine
Default install of each framework
Recommended way of outputting text
Prints “Hello World”
ApacheBench test on number of connections over 10 second period
Ran on php 5.6 and php 7 beta
I tried to be as objective as possible
However might have given away my preferences
I use mostly use one of these Full Stack and one Micro
Can anyone guess which Full Stack?
Can anyone guess which micro?
Tough question to answer
“To each his own”
New users I’d probably recommend Laravel
More experienced and those looking to try something a bit harder Symfony2
You should try a new framework, don’t stick with what you know
Try a microframework to compliment your projects