cakephpstudy5 hacks

of 31 /31
ココココココココ コココココココココ コ 5 コ CakePHP コココ @Tokyo ON 2010/5/29( コ )

Author: hiroki-shimizu

Post on 13-Dec-2014




1 download

Embed Size (px)




Page 1: Cakephpstudy5 hacks


第 5 回 CakePHP 勉強会@Tokyo

ON 2010/5/29( 土 )

Page 2: Cakephpstudy5 hacks


Page 3: Cakephpstudy5 hacks

About me Name: 清水 紘己( Shimizu Hiroki ) Call me hiromi ! ( @hiromi2424 ) Action about CakePHP

TranslationNew features reportTransitionComponentFew Patches/Tickets

I love singing, Ramen and Beer!

Page 4: Cakephpstudy5 hacks

INDEX Introduction Hacks


Page 5: Cakephpstudy5 hacks


Page 6: Cakephpstudy5 hacks

Why hack?

Embedding Application Policy

Page 7: Cakephpstudy5 hacks

Why hack?

Deep Debugging

Page 8: Cakephpstudy5 hacks

Why hack?

For Stub (Mock object)

Page 9: Cakephpstudy5 hacks

How hack? Hook object mapping like alias

This session focuses this Provided hook

Page 10: Cakephpstudy5 hacks

Userland import App/libs/* Copy the file from Core to App Replace, done

Dirty Hack

Page 11: Cakephpstudy5 hacks

Impossible/Non-elegance Static Classes Basic functions

Use Lithium

Page 12: Cakephpstudy5 hacks


Page 13: Cakephpstudy5 hacks

Hack components

Page 14: Cakephpstudy5 hacks

Components - ACL Provided hook Configure::write('Acl.classname‘, ‘MyAcl’) MyAclComponent extends AclBase Implements methods

… lots of.

Page 15: Cakephpstudy5 hacks

Components - Others Replaces mapped objects Component::_loaded[$key]

Reference to object Hook timing


Page 16: Cakephpstudy5 hacks

Components - Others

init() initialize() startup()


Component class flow

Mapping objects



Page 17: Cakephpstudy5 hacks

Components - Others

Auth::__construct() Auth::initialize()

$loaded[‘Session’] Existent

Existent$loaded[‘Auth’] Undefined

$loaded[‘Security’] Undefined

Session initialized NO YES

Auth initialized NO Initializing now

Security initialized NO NO

var $components = (‘Session’, ’Auth’, ‘Security’);

Page 18: Cakephpstudy5 hacks

Components - Others $Component->enabled = boolean SessionComponent::__construct($base) Workaround double initialized Import original settings


Page 19: Cakephpstudy5 hacks

Hack helpers

Page 20: Cakephpstudy5 hacks

Helpers Replaces mapped objects $view::loaded[$key]

Reference to object Hook timing

beforeRender() View::_loadHelpers()


Page 21: Cakephpstudy5 hacks

Hacks Models

Page 22: Cakephpstudy5 hacks

Models ClassRegistry::init() alias option Example

ClassRegistry::init(array(‘class’ => ‘MockPost’,‘alias’ => ‘Post’,

)) Initialization timing

Before Controller::constructClasses

Page 23: Cakephpstudy5 hacks

Hacks CakeRoute

Page 24: Cakephpstudy5 hacks

CakeRoute From 1.3 (Plugin | App)/libs/routes/my_route.php App::import(‘Lib’, ‘routes/MyRoute’) Router::connect(…, …, array(‘routeClass’ => ‘MyRoute’))

class MyRoute extends CakeRoute Implement methods


Page 25: Cakephpstudy5 hacks

CakeRoute parse()

Parsing given URL to parameters.Helping additional parameters

Page 26: Cakephpstudy5 hacks

CakeRoute match()

Convert parameters to proper URL.Reverse RoutingHelping generation of URL

Page 27: Cakephpstudy5 hacks

CakeRoute Example

Mark Story wrote great example on his BLOG I added reverse routing

Page 28: Cakephpstudy5 hacks


Page 29: Cakephpstudy5 hacks

Cache (Plugin | App)/libs/cache/my_cache.php Cache::config(‘default’, array(‘engine’ => MyCache))

class MyCache extends CacheEngine Implement methods


Page 30: Cakephpstudy5 hacks

CakeLog From 1.3 (Plugin | App)/libs/log/my_logger.php CakeLog::config(‘my’, array(‘engine’ => MyCache))

class MyLogger Implement methods

write() only Example

if($type == ‘error’) file logging and mail to admin

Page 31: Cakephpstudy5 hacks


Hacks rarely needed? CakePHP is realy needed? Learn Possible/Impossible CakePHP2.0 features -> alias