Created
August 25, 2012 18:39
-
-
Save cgutierrez/3469048 to your computer and use it in GitHub Desktop.
Lithium Presentation Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Email { | |
protected $logger; | |
public function __construct(Logger $logger = null) { | |
$this->setLogger($logger); | |
} | |
public function send($to) { | |
$this->getLogger()->write('Sending email to ' . $to); | |
mail($to, 'Subject', 'Hello Email'); | |
} | |
public function setLogger(Logger $logger) { | |
$this->logger = $logger; | |
} | |
public function getLogger() { | |
if (!$this->logger) { | |
$this->logger = new Logger(); | |
} | |
return $this->logger; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$logger = new Logger(); | |
aop_add_before('Email->send()', function(AopTriggeredJoinpoint $joinPoint) use ($logger) { | |
$args = $joinPoint->getArguments(); | |
$logger->write('Sending email to ' . $args[0]); | |
}); | |
class Email { | |
public function send($to) { | |
mail($to, 'Subject', 'Hello Email'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$logger = new Logger(); | |
$email = new Email(); | |
$email->applyFilter('send', function($self, $params, $chain) use ($logger) { | |
$logger->write('Sending email to ' . $args[0]); | |
return $chain->next($self, $params, $chain); | |
}); | |
StaticEmail::applyFilter('send', function($self, $params, $chain) use ($logger) { | |
$logger->write('Sending email to ' . $args[0]); | |
return $chain->next($self, $params, $chain); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ExampleController extends \lithium\action\Controller { | |
public function __construct(array $config = []) { | |
$config['render'] = [ | |
'negotiate' => true | |
]; | |
parent::__construct($config); | |
} | |
public function action() { | |
return ['key' => 'val']; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ExampleController extends \lithium\action\Controller { | |
public function action() { | |
$this->set(['key' => 'val']); | |
return $this->render(['type' => 'json']); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Email extends \lithium\core\Object { | |
public function send($to) { | |
return $this->_filter(__METHOD__, compact('to'), function($self, $params) { | |
extract($params); | |
mail($to, 'Subject', 'Hello Email'); | |
}); | |
} | |
} | |
class StaticEmail extends \lithium\core\StaticObject { | |
public static function send($to) { | |
return static::_filter(__FUNCTION__, compact('to'), function($self, $params) { | |
extract($params); | |
mail($to, 'Subject', 'Hello Email'); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use lithium\net\http\Router; | |
use lithium\action\Dispatcher; | |
Dispatcher::config([ | |
'rules' => [ | |
'admin' => [ | |
'controller' => 'app\controllers\admin\{:controller}Controller' | |
] | |
] | |
]); | |
Router::connect('/admin/{:args}', ['admin' => true], [ | |
'continue' => true, | |
'handler' => function($request) { | |
// modify the request then continue to the next route | |
return $request; | |
} | |
]); | |
Router::connect('/{:controller}/{:action}/{:args}'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use lithium\net\http\Router; | |
use lithium\action\Response; | |
Router::connect('/example/{:args}', [], function($request) { | |
return new Response(['body' => "Hello!"]); | |
}); | |
Router::connect('/create', ['http:method' => 'POST'], function($request){ | |
// handle POST request | |
}); | |
Router::connect('/create', ['http:method' => 'GET'], function($request){ | |
// handle GET request | |
}); | |
Router::connect('/create', ['http:host' => 'manage.example.com'], function($request){ | |
// handle GET request | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Media::type('json', 'application/json', [ | |
'layout' => false, | |
'encode' => 'json_encode', | |
'decode' => 'json_decode' | |
]); | |
Media::type('csv', 'application/csv', [ | |
'encode' => function($data) { | |
ob_start(); | |
$out = fopen('php://output', 'w'); | |
foreach ((array) $data['csv'] as $row) { | |
fputcsv($out, $row); | |
} | |
fclose($out); | |
return ob_get_clean(); | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment