making the most out of cakephp 2.2

Post on 17-May-2015

3.236 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

EVENT MANAGER

PUBLISHER - SUBSCRIBERS

• Made the code we had for behaviors, helpers, components, etc. into a reusable generic events system

• The idea is simple: an object publishes a named event with some data and an unknown list of listeners will get notified and have the chance of changing the outcome of the original operation

SOME BUILT-IN EVENTS

• Model.beforeValidate, Model.afterValidate, Model.beforeSave, Model.afterSave...

• Controller.initialize, Controller.startup, Controller.beforeRender...

• View.beforeRender, View.beforeLayout...

• Dispatcher.beforeDispatch, Dispatcher.afterDispatch

CREATE YOUR OWN EVENTS!

• A Paypal plugin received a notification from payments server. What do you do? (You need to send emails, mark order s processed, decrement stock...)

• Trigger a new Paypal.afterProcess event!

• Any number of listeners can processes the rest of the actions for you while leaving the Paypal plugin free of any external concerns

A SIMPLE EXAMPLE

SUBSCRIPTION PATTERNS

• Direct: during runtime an object attaches event listeners to another as needed

• Configuration based: define a list or listeners. Read the list in the publisher class or factory when needed

• Indirect global: Use the global event manager to attach indirectly to any event.

• Convention based: load listeners for an object from a convention-defined location

USING THE GLOBAL MANAGER

DISPATCHER FILTERSLet’s jump directly to examples :)

AN ASSET COMPRESSOR

A RESPONSE CACHER

SOME COOL IDEAS WITH FILTERS

• Complete reverse proxy

• Pluggable OAuth Authorization

• Performance monitoring

HTTP CACHING

TYPES OF CACHE

• Browser caches

• Proxy caches

• Gateway caches (or reverse proxy)

HEADERS INVOLVED IN CACHING

• Cache-Control - This it the most important one

• Expires

• ETag

• Last-Modified

MESSING WITH CACHE-CONTROL

Proxies use shared cache, so setting a response as private will not be cached by

default

VALIDATION VS EXPIRATION

• Expiration model: you specify for how long a response is valid, new requests will not be made until time expires

• Validation: requests are made every time and server responds with either a 304 status or a new response.

MESSING WITH EXPIRATION

Using max-age and s-maxage is preferred because of several limitations in the Expire header

VALIDATION MODEL

If browser send If-Modified-Since header, then a 304response will be issued and view rendering process will be skipped entirely

CONDITIONAL VIEW RENDER

Same as previous example, but automatic and awesome

CACHE GROUPS

WHAT ARE THEY?

• A declarative, static way of tagging cache entries with any string

• Entries tagged with the same string can be invalidated all at the same time in an atomic way

• A way to organize your cache entries into logical groups

A SIMPLE EXAMPLE

CACHE GROUPS FACTS

• Clearing all entries under a groups will delete all entries for all Cache configurations having the same groups in them and sharing the same prefix. Even across engines!

• You cannot dynamically add new groups to cache configurations, you need to plan your strategy

• You *could* dynamically create cache configs having exactly the groups you want to tag entries with, but it’s super hard

MODEL VALIDATOR

WHAT FOR?

• Some applications require dynamic validations rules depending on the action being executed

• Directly modifying $validate property on the model can be tricky

• Several parts of CakePHP had to implement the same $validates parsing to extract information out of it

WHAT CAN I DO WITH IT?

• Dynamically inspect, add or remove validations rules for a model

• Swap at runtime validation objects depending on your own logic

• Create with an easy API complex validation systems

ADDING NEW RULES

MODIFYING RULES

DELETING RULES

USING ARRAY ACCESS

top related