Software Design Patterns
in Laravel 4
by Phill Sparks
Credits
• Rosalind Goodall - Graphics
• Taylor Otwell - Laravel
@PhillSparks
@PhillSparks
•API Engineer at CrowdLab
@PhillSparks
•API Engineer at CrowdLab
•From Leicester, UK
@PhillSparks
•API Engineer at CrowdLab
•From Leicester, UK
•Climbing Instructor
@PhillSparks
•API Engineer at CrowdLab
•From Leicester, UK
•Climbing Instructor
•Scout
@PhillSparks
•API Engineer at CrowdLab
•From Leicester, UK
•Climbing Instructor
•Scout
•Laravel Core Team
Laravel
• With Laravel since 1.5.9
• Core Team Member
• Past: Quality Team
• Now: Community engagement and support
Laravel
• With Laravel since 1.5.9
• Core Team Member
• Past: Quality Team
• Now: Community engagement and support
• Dayle still owes me 9000 support points!
Software Design Patterns
Each pattern describes a problem which occurs over
and over again in our environment, and then describes
the core of the solution to that problem, in such a
way that you can use this solution a million times
over, without ever doing it the same way twice.
-- Christopher Alexander, AIS+77
“ ”
Software Design Patterns
Software Design Patterns
The Patterns
• Builder
• Chain of Responsibility
• Command
• Facade
• Factory
• Iterator
• Mediator
• Observer
• Presenter
• Repository
• Singleton
• Strategy
Notations
Class
Notations
Sub-class
Notations
Abstract Class
Notations
Class Instance
Notations
Implementation
Notations
Building Blocks
Interfaces
namespace IlluminateAuth;
interface UserInterface {
public function getAuthIdentifier();
public function getAuthPassword();
}
class GenericUser implements UserInterface {
• 244 interfaces in Laravel (including vendors)
• 25 from Laravel Core
• 65 from Symfony
• 73 from Swiftmailer
Interface
Abstract Classes
namespace IlluminateDatabaseEloquentRelations;
abstract class Relation {
abstract public function addConstraints();
abstract public function getResults();
}
class BelongsTo extends Relation {
Abstract Classes
• 128 abstract classes in Laravel (including
vendors)
• 14 from Laravel Core
• 15 from Swiftmailer
• 40 from Symfony
Iterator
PHP’s Iterator Interface
Laravel’s Iterators
• Collection
• Paginator
Observer
Dispatcher
namespace IlluminateEvents;
Dispatcher
namespace IlluminateEvents;
class LoginHandler {
function handle($user) {
// do something with $data
}
}
Event::listen(‘user.login’, ‘LoginHandler’);
Event::fire(‘user.login’, $user);
Singleton
AliasLoader
namespace IlluminateFoundation;
Multiton
DatabaseManager
namespace IlluminateDatabase;
Facade
Facade
namespace IlluminateSupportFacades;
Input
namespace IlluminateSupportFacades;
Route
namespace IlluminateSupportFacades;
Response
namespace IlluminateSupportFacades;
Str
namespace IlluminateSupport;
Facade vs Singleton
Decorator
BootstrapPresenter
namespace IlluminatePagination;
BootstrapPresenter
namespace IlluminatePagination;
Repository
• Examples:
• IlluminateCacheRepository
• IlluminateConfigRepository
Builder (aka Manager)
• Examples:
• IlluminateAuthAuthManager
• IlluminateCacheCacheManager
• IlluminateQueueQueueManager
• IlluminateSessionSessionManager
Factory
• Examples:
• IlluminateDatabaseDatabaseManager
• IlluminateDatabaseConnectors
ConnectionFactory
• IlluminateValidationFactory
Service Provider
• Core to Laravel’s IoC
• See IlluminateSupportServiceProvider
• Examples:
• IlluminateAuthAuthServiceProvider
• IlluminateHashHashServiceProvider
• IlluminateLogLogServiceProvider
Strategy
• Examples:
• IlluminateCacheStoreInterface
• IlluminateConfigLoaderInterface
• IlluminateDatabase - Builders & Grammars
• IlluminateTranslationLoaderInterface
• IlluminateViewViewFinderInterface
s/PhillSparks//

Software Design Patterns in Laravel by Phill Sparks