Slide 1

Slide 1 text

Alloy Framework PHP 5.3+ Modular Hierarchical MVC http://alloyframework.org Wednesday, March 30, 2011

Slide 2

Slide 2 text

Vance Lucas (@vlucas) http://alloyframework.org Who am I? Vance Lucas http://vancelucas.com @vlucas Business: http://actridge.com PHP dev since 1999 (PHP 3) I love good OOP code and concepts, but hate the complexity it brings 2 Wednesday, March 30, 2011

Slide 3

Slide 3 text

A Few Points... Kind of a disclaimer, but not really Wednesday, March 30, 2011

Slide 4

Slide 4 text

Vance Lucas (@vlucas) http://alloyframework.org You May NOT Like Alloy If... You are an Object-Oriented Programming Purist You don’t care how complex code is as long as it’s “correct” OOP (because some guy’s book said so) You LOVE using Zend Framework, Symfony, or FLOW3 You LOVE XML and Dependency Injection Containers You are looking for a component library 4 Wednesday, March 30, 2011

Slide 5

Slide 5 text

Vance Lucas (@vlucas) http://alloyframework.org You MIGHT Like Alloy If... You think frameworks like Zend, Symfony, or FLOW3 make some things too difficult and cumbersome You don’t think any PHP framework has it quite right You don’t like “too much magic” You want to know what your framework is doing You value ease of use over everything else You want good OOP code that follows known coding standards that is still easy to use and understand 5 Wednesday, March 30, 2011

Slide 6

Slide 6 text

Vance Lucas (@vlucas) http://alloyframework.org Philosophy Usefulness, Simplicity and Strong Design Minimum code, maximum impact Use OOP principles and design, but don’t over-do it PHP is not Java with dollar signs Explicitness > “Magic” Easier to understand & see what is going on Design patterns should never trump user experience or get in the way Users over “correctness” 6 Wednesday, March 30, 2011

Slide 7

Slide 7 text

Vance Lucas (@vlucas) http://alloyframework.org Alloy Framework Goals Lightweight Modular Organization Hierarchical MVC (HMVC) Easy to understand and use Easy to make REST APIs and HTML pages No config/setup necessary, works anywhere Follow PEAR/Zend Coding Standards Strike a balance between good OOP concepts and ease of use with low learning curve 7 Wednesday, March 30, 2011

Slide 8

Slide 8 text

Alloy Architecture Inside & Out Wednesday, March 30, 2011

Slide 9

Slide 9 text

Vance Lucas (@vlucas) http://alloyframework.org Alloy Architecture Overview Modular Organization Kernel Plugins Hierarchical MVC (HMVC) Controllers as Resources Events and Filters 9 Wednesday, March 30, 2011

Slide 10

Slide 10 text

Vance Lucas (@vlucas) http://alloyframework.org Modular Organization All related files in a single top-level named folder No separate “controllers”, “models” and “views” directories at same level Unlimited levels of nesting Easier distributable packages 10 Wednesday, March 30, 2011

Slide 11

Slide 11 text

Vance Lucas (@vlucas) http://alloyframework.org Kernel Core object available from anywhere in your app \Kernel() - only global-scope function in Alloy Multiple uses: Config access Lazy-loading Factory / Service Locator Extension Point for Modules and Plugins Sole Dependency 11 Wednesday, March 30, 2011

Slide 12

Slide 12 text

Vance Lucas (@vlucas) http://alloyframework.org Kernel - Config Access Getter Setter 12 Wednesday, March 30, 2011

Slide 13

Slide 13 text

Vance Lucas (@vlucas) http://alloyframework.org Kernel - Lazy-Load Factory Write This: Not That: Or This: 13 Wednesday, March 30, 2011

Slide 14

Slide 14 text

Vance Lucas (@vlucas) http://alloyframework.org Kernel - Extension Point Proxies to callbacks added at runtime via __call Enables all kinds of custom functionality Factory methods for loading & using 3rd party libs Exposing plugin methods for global use Helper or utility methods 14 Wednesday, March 30, 2011

Slide 15

Slide 15 text

Plugins The primary way of extending Alloy Wednesday, March 30, 2011

Slide 16

Slide 16 text

Vance Lucas (@vlucas) http://alloyframework.org Plugin - Symfony2 Finder File: app/Plugin/Finder/Plugin.php Libs: app/Plugin/Finder/lib/Symfony/Component/ ... Add finder method to Kernel for use 16 Wednesday, March 30, 2011

Slide 17

Slide 17 text

Vance Lucas (@vlucas) http://alloyframework.org Plugin - Add to Config File: app/config/app.php 17 Wednesday, March 30, 2011

Slide 18

Slide 18 text

Vance Lucas (@vlucas) http://alloyframework.org Plugin - Usage Use finder method on Kernel instance 18 Wednesday, March 30, 2011

Slide 19

Slide 19 text

Hierarchical MVC Solves the “widget problem” Wednesday, March 30, 2011

Slide 20

Slide 20 text

Vance Lucas (@vlucas) http://alloyframework.org Hierarchical MVC Multiple MVC dispatches to fulfill a single request Solves the “widget problem” Sidebar content can be self-contained module Ads, tag clouds, blog headlines, etc. Encourages module re-use & helps DRY Not strictly “hierarchical” - more like nested Hierarchy is not tracked or enforced in any way 20 Wednesday, March 30, 2011

Slide 21

Slide 21 text

Vance Lucas (@vlucas) http://alloyframework.org 21 Wednesday, March 30, 2011

Slide 22

Slide 22 text

Vance Lucas (@vlucas) http://alloyframework.org 22 Wednesday, March 30, 2011

Slide 23

Slide 23 text

Controllers Respond to both internal and external requests Wednesday, March 30, 2011

Slide 24

Slide 24 text

Vance Lucas (@vlucas) http://alloyframework.org Controllers Part of the Module package that handles requests Special name “scope” for web-accessible actions GET = Action POST, PUT, DELETE, etc = Method Ensures HTTP methods are required for access Alloy\Request object is first parameter All named params from route set on Request object 24 Wednesday, March 30, 2011

Slide 25

Slide 25 text

Vance Lucas (@vlucas) http://alloyframework.org Controllers (cont’d) NOTHING is automatically or implicitly loaded No views, models, or anything else Explicitly return response or content to send Controllers are not factories Controllers are extremely lightweight Controllers do not hold the context of their requests 25 Wednesday, March 30, 2011

Slide 26

Slide 26 text

Vance Lucas (@vlucas) http://alloyframework.org Example REST Controller Notice Action vs Method 26 Wednesday, March 30, 2011

Slide 27

Slide 27 text

Controller Response Types Sending output and manipulating control flow Wednesday, March 30, 2011

Slide 28

Slide 28 text

Vance Lucas (@vlucas) http://alloyframework.org Simple Strings Sends 200 OK status with string content as body 28 Wednesday, March 30, 2011

Slide 29

Slide 29 text

Vance Lucas (@vlucas) http://alloyframework.org Any object with __toString Sends 200 OK status with string content as body 29 Wednesday, March 30, 2011

Slide 30

Slide 30 text

Vance Lucas (@vlucas) http://alloyframework.org View Templates Most common Module response type Returns object - template not yet rendered 30 Wednesday, March 30, 2011

Slide 31

Slide 31 text

Vance Lucas (@vlucas) http://alloyframework.org Resource Objects Auto-converts array to JSON or XML for API response Accepts objects with toArray methods 31 Wednesday, March 30, 2011

Slide 32

Slide 32 text

Vance Lucas (@vlucas) http://alloyframework.org Generic Response Body + HTTP Status Base Alloy\Module\Response that view templates and resources extend from Can set custom layout, errors, headers, etc. using a fluent interface just like templates and resources 32 Wednesday, March 30, 2011

Slide 33

Slide 33 text

Vance Lucas (@vlucas) http://alloyframework.org Boolean False Generic 404 (Throws Alloy\Exception\FileNotFound) Plugins can filter on ‘dispatch_exception’ event to produce a custom global response 33 Wednesday, March 30, 2011

Slide 34

Slide 34 text

Why Explicit Returns? Easier to manipulate responses and control flow Wednesday, March 30, 2011

Slide 35

Slide 35 text

Vance Lucas (@vlucas) http://alloyframework.org Easier to re-use Actions Re-use form template from ‘newAction’ No forwarding or other indirection necessary Object return lets you modify it further before display through fluent interface 35 Wednesday, March 30, 2011

Slide 36

Slide 36 text

Vance Lucas (@vlucas) http://alloyframework.org Modify Dispatches at Will Template object returned is not yet rendered so we can modify it at will Change path to a local template Change layout Add errors Change response code or headers, etc. Very flexible 36 Wednesday, March 30, 2011

Slide 37

Slide 37 text

Vance Lucas (@vlucas) http://alloyframework.org ... Even pull out values Since the template is not yet rendered, we can even pull out values that have been set and re-use them however we want to We don’t even have to render the dispatch result at all No wasted resources 37 Wednesday, March 30, 2011

Slide 38

Slide 38 text

URL Routing Simple named parameters, full control Wednesday, March 30, 2011

Slide 39

Slide 39 text

Vance Lucas (@vlucas) http://alloyframework.org Parameter Types <:alpha> Matches [a-zA-Z0-9\_\-\+\%\s]+ <#numeric> Matches [0-9]+ <*wildcard> Marches .* Does NOT split the URL by “segment” 39 Wednesday, March 30, 2011

Slide 40

Slide 40 text

Vance Lucas (@vlucas) http://alloyframework.org Some Default Routes Fluent interface allows route modification REST verb params will override defaults and matches POST /module/add => Module::postMethod 40 Wednesday, March 30, 2011

Slide 41

Slide 41 text

Vance Lucas (@vlucas) http://alloyframework.org Custom Routes Can be literally anything URL does not have to map / Example from Autoridge.com /<#year>/<:make>/<:model> 41 Wednesday, March 30, 2011

Slide 42

Slide 42 text

Vance Lucas (@vlucas) http://alloyframework.org Static Routes Map arbitrary static path to Module::action No PREG matching overhead Path can include slashes and be as long as necessary Router does not split URL into segments 42 Wednesday, March 30, 2011

Slide 43

Slide 43 text

Making an App HTML + JSON API, single Controller method Wednesday, March 30, 2011

Slide 44

Slide 44 text

Vance Lucas (@vlucas) http://alloyframework.org app/Module/Blog/Post.php Using Spot ORM (Ships with Alloy, but NOT required) Spot provided with a plugin, not tied to Alloy in any way 44 Wednesday, March 30, 2011

Slide 45

Slide 45 text

Vance Lucas (@vlucas) http://alloyframework.org Blog Example app/Module/Blog/Controller.php 45 Wednesday, March 30, 2011

Slide 46

Slide 46 text

Vance Lucas (@vlucas) http://alloyframework.org app/Module/Blog/views/index.html.php 46 Wednesday, March 30, 2011

Slide 47

Slide 47 text

Vance Lucas (@vlucas) http://alloyframework.org http://localhost/path/blog 47 Wednesday, March 30, 2011

Slide 48

Slide 48 text

Vance Lucas (@vlucas) http://alloyframework.org Magic Datagrid? Alloy has View Generics Extensions of view templates Generic HTML template markup with custom functions that accept Closures to enable fully custom PHP markup in designated areas (like table cells) with no special syntax or recursive includes You don’t have to use them, but they can be huge time-savers if you do 48 Wednesday, March 30, 2011

Slide 49

Slide 49 text

Vance Lucas (@vlucas) http://alloyframework.org Adding an API Template for HTML, Resource object for API 49 Wednesday, March 30, 2011

Slide 50

Slide 50 text

Vance Lucas (@vlucas) http://alloyframework.org http://localhost/path/blog/index.json Array -> JSON via Resource response type 50 Wednesday, March 30, 2011

Slide 51

Slide 51 text

Vance Lucas (@vlucas) http://alloyframework.org API Errors? 51 Wednesday, March 30, 2011

Slide 52

Slide 52 text

Vance Lucas (@vlucas) http://alloyframework.org HTML JSON 52 Wednesday, March 30, 2011

Slide 53

Slide 53 text

Layouts & Blocks Make your App Look Nice Wednesday, March 30, 2011

Slide 54

Slide 54 text

Vance Lucas (@vlucas) http://alloyframework.org Layouts Really just another view template with a special location Includes both header & footer, content placed inside 54 Wednesday, March 30, 2011

Slide 55

Slide 55 text

Vance Lucas (@vlucas) http://alloyframework.org Blocks Named regions that content can be pushed to from any view template in your app Static to current request so no data has to be passed 55 Wednesday, March 30, 2011

Slide 56

Slide 56 text

Vance Lucas (@vlucas) http://alloyframework.org app/layouts/app.html.php 56 Wednesday, March 30, 2011

Slide 57

Slide 57 text

Vance Lucas (@vlucas) http://alloyframework.org app/Module/Blog/views/viewAction.html.php Post tag list in layout sidebar 57 Wednesday, March 30, 2011

Slide 58

Slide 58 text

Vance Lucas (@vlucas) http://alloyframework.org app/Module/Blog/views/viewAction.html.php HMVC dispatches as “widgetized” content Promotes better code re-use, DRY principle, allows polymorphic associations for generic types of content 58 Wednesday, March 30, 2011

Slide 59

Slide 59 text

Events & Filters Dynamic Behavior & Manipulating Responses Wednesday, March 30, 2011

Slide 60

Slide 60 text

Vance Lucas (@vlucas) http://alloyframework.org Layout Plugin Layout wrapping is achieved with a plugin in Alloy Filters ‘dispatch_content’ event 60 Wednesday, March 30, 2011

Slide 61

Slide 61 text

Vance Lucas (@vlucas) http://alloyframework.org Main Dispatch Content Filter Many other events and filters not shown in this diagram 61 Wednesday, March 30, 2011

Slide 62

Slide 62 text

Vance Lucas (@vlucas) http://alloyframework.org Layout Plugin - wrapLayout Returns template wrapped in template 62 Wednesday, March 30, 2011

Slide 63

Slide 63 text

Vance Lucas (@vlucas) http://alloyframework.org Thanks! Alloy on the web: http://alloyframework.org http://github.com/alloyphp Vance Lucas Personal: http://vancelucas.com Business: http://actridge.com 63 Wednesday, March 30, 2011