Aura for PHP

Hari KT
@harikt
http://harikt.com
FOSSMeet 2014 @ NIT Calicut
About me
●

Freelance LAMP developer

●

Open-source lover and contributor

●

Writes for sitepoint.com

●

Technical revi...
Aura
●

2nd major version of SolarPHP ( started in
2004 by Paul M Jones)

●

Independent decoupled library packages

●

V1...
PSR-0 vs PSR-4
namespace AuraRouter;
class RouterFactory {
}
●

PSR-0 : http://www.php-fig.org/psr/psr-0/
AuraRouterRouteF...
Driving Principles
●
●

Libraries first, framework 2nd
No use of globals within packages
( $_SERVER, $_GET etc )

●

No de...
Library / Package Organization

FOSSMeet 2014 @ NIT Calicut
Aura.Router v2

FOSSMeet 2014 @ NIT Calicut
Aura.Router
●
●

Provides a web router implementation
Given a URL path and a copy of $_SERVER, it
will extract path-info p...
Instantiation
require path/to/Aura.Router/autoload.php;
use AuraRouterRouterFactory;
$router_factory = new RouterFactory;
...
Adding Route
●

// add a simple unnamed route with params
$router->add(null, '/{controller}/{action}/{id}');

●

// add a ...
Matching A Route
// get the incoming request URL path
$path = parse_url($_SERVER['REQUEST_URI'],
PHP_URL_PATH);
// get the...
Example
$path = '/blog/read/42.json';
$route = $router->match($path, $_SERVER);
var_export($route->params);
[
'controller'...
Dispatching a Route
if ($route) {
echo "No application route was found for that URL path.";
exit();
}
$class = $route->par...
Microframework Route
$router->add('read', '/blog/read/{id}')
->addTokens(array( 'id' => 'd+', ))
->addValues(array(
'contr...
Microframework Dispatcher
$controller = $route->params['controller'];
echo $controller($route->params);

FOSSMeet 2014 @ N...
Generating a route path
// $path => "/blog/read/42.atom"
$path = $router->generate('read', array(
'id' => 42,
'format' => ...
Aura.Filter v1

FOSSMeet 2014 @ NIT Calicut
Instantiation
$filter = require "/path/to/Aura.Filter/scripts/instance.php";
or
use AuraFilterRuleCollection as Filter;
us...
Rule Types
●

●

●

Soft : if the rule fails, the filter will keep applying all
remaining rules to that field and all othe...
Aura.Filter
$filter->addSoftRule('username', $filter::IS, 'alnum');
$filter->addSoftRule('username', $filter::IS, 'strlenB...
Validating and Sanitizing
●

●

●

●

●

RuleCollection::IS means the field value must match the rule.
RuleCollection::IS_...
Available Rules
alnum

alpha

between

blank

creditCard

dateTime

email

equalToField

equalToValue

float

inKeys

inVa...
Custom Rules
●

Write a rule class

●

Set that class as a service in the RuleLocator

●

Use the new rule in our filter c...
Rule Class
●

●

●

●

●

Extend AuraFilterAbstractRule with two methods:
validate() and sanitize()
Add params as needed t...
Example
use AuraFilterAbstractRule;
class Hex extends AbstractRule {
protected $message;
public function validate($max = n...
Questions ?
●

Hire me to coach you PHP, Aura
Hari KT
harikt.com
https://github.com/harikt
FOSSMeet 2014 @ NIT Calicut
more library / framework
Aura.Cli

Aura.Di

Aura.Includer

Aura.Input

Aura.Web

Aura.Di

Aura.Dispatcher

Aura. Sql

Aura...
PHP User Group in Calicut
●

●
●

Plan to organize a user group meetup once in a
month
Looking for space and audience
Join...
Thanks
●

Website : http://auraphp.com/

●

Source Code : http://github.com/auraphp

●

●
●

●

Groups :
https://groups.go...
Upcoming SlideShare
Loading in...5
×

Aura for PHP at Fossmeet 2014

2,187

Published on

Introduce aura project a collection of standalone library package for PHP

Published in: Technology
2 Comments
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total Views
2,187
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
3
Comments
2
Likes
0
Embeds 0
No embeds

No notes for slide

Aura for PHP at Fossmeet 2014

  1. 1. Aura for PHP Hari KT @harikt http://harikt.com FOSSMeet 2014 @ NIT Calicut
  2. 2. About me ● Freelance LAMP developer ● Open-source lover and contributor ● Writes for sitepoint.com ● Technical reviewer of Packt FOSSMeet 2014 @ NIT Calicut
  3. 3. Aura ● 2nd major version of SolarPHP ( started in 2004 by Paul M Jones) ● Independent decoupled library packages ● V1 started around late 2010 ( PSR-0 standard ) ● V2 started around late 2013 ( PSR-4 standard ) FOSSMeet 2014 @ NIT Calicut
  4. 4. PSR-0 vs PSR-4 namespace AuraRouter; class RouterFactory { } ● PSR-0 : http://www.php-fig.org/psr/psr-0/ AuraRouterRouteFactory => /path/to/src/Aura/Router/RouteFactory.php Zend_Mail_Message => /path/to/lib/Zend/Mail/Message.php ● PSR-4 : http://www.php-fig.org/psr/psr-4/ AuraRouterRouteFactory => /path/to/src/RouteFactory.php FOSSMeet 2014 @ NIT Calicut
  5. 5. Driving Principles ● ● Libraries first, framework 2nd No use of globals within packages ( $_SERVER, $_GET etc ) ● No dependency on any other package ● Tests and Assets inside the package FOSSMeet 2014 @ NIT Calicut
  6. 6. Library / Package Organization FOSSMeet 2014 @ NIT Calicut
  7. 7. Aura.Router v2 FOSSMeet 2014 @ NIT Calicut
  8. 8. Aura.Router ● ● Provides a web router implementation Given a URL path and a copy of $_SERVER, it will extract path-info parameters and $_SERVER values for a specific route FOSSMeet 2014 @ NIT Calicut
  9. 9. Instantiation require path/to/Aura.Router/autoload.php; use AuraRouterRouterFactory; $router_factory = new RouterFactory; $router = $router_factory->newInstance(); or use AuraRouterRouter; use AuraRouterRouteCollection; use AuraRouterRouteFactory; $router = new Router(new RouteCollection(new RouteFactory)); FOSSMeet 2014 @ NIT Calicut
  10. 10. Adding Route ● // add a simple unnamed route with params $router->add(null, '/{controller}/{action}/{id}'); ● // add a route with optional params $router->add('archive', '/archive{/year,month,day}') ● // add a named route with an extended specification $router->add('read', '/blog/read/{id}{format}') ->addTokens(array('id' => 'd+', 'format' => '(.[^/]+)?',)) ->addValues(array('controller' => 'BlogPage', 'action' => 'read', 'format' => '.html',)); ● // REST resource route $router->attachResource('blog', '/blog'); FOSSMeet 2014 @ NIT Calicut
  11. 11. Matching A Route // get the incoming request URL path $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); // get the route based on the path and server $route = $router->match($path, $_SERVER); FOSSMeet 2014 @ NIT Calicut
  12. 12. Example $path = '/blog/read/42.json'; $route = $router->match($path, $_SERVER); var_export($route->params); [ 'controller' => 'BlogPage', 'action' => 'read', 'id' => 42, 'format' => 'json' ] FOSSMeet 2014 @ NIT Calicut
  13. 13. Dispatching a Route if ($route) { echo "No application route was found for that URL path."; exit(); } $class = $route->params['controller']; // BlogPage $method = $route->params['action']; // read // instantiate the controller class $object = new $class(); echo $object->$method($route->params); // $object->read($route->params); FOSSMeet 2014 @ NIT Calicut
  14. 14. Microframework Route $router->add('read', '/blog/read/{id}') ->addTokens(array( 'id' => 'd+', )) ->addValues(array( 'controller' => function ($params) { $id = (int) $params['id']; header('Content-Type: application/json'); echo json_encode(['id' => $id]); }, )); FOSSMeet 2014 @ NIT Calicut
  15. 15. Microframework Dispatcher $controller = $route->params['controller']; echo $controller($route->params); FOSSMeet 2014 @ NIT Calicut
  16. 16. Generating a route path // $path => "/blog/read/42.atom" $path = $router->generate('read', array( 'id' => 42, 'format' => '.atom', )); $href = htmlspecialchars($path, ENT_QUOTES, 'UTF-8'); echo '<a href="$href">Atom feed for this blog entry</a>'; FOSSMeet 2014 @ NIT Calicut
  17. 17. Aura.Filter v1 FOSSMeet 2014 @ NIT Calicut
  18. 18. Instantiation $filter = require "/path/to/Aura.Filter/scripts/instance.php"; or use AuraFilterRuleCollection as Filter; use AuraFilterRuleLocator; use AuraFilterTranslator; $filter = new Filter( new RuleLocator, new Translator(require 'path/to/Aura.Filter/intl/en_US.php') ); FOSSMeet 2014 @ NIT Calicut
  19. 19. Rule Types ● ● ● Soft : if the rule fails, the filter will keep applying all remaining rules to that field and all other fields Hard : if the rule fails, the filter will not apply any more rules to that field, but it will keep filtering other fields. Stop : if the rule fails, the filter will not apply any more filters to any more fields; this stops all filtering on the data object. FOSSMeet 2014 @ NIT Calicut
  20. 20. Aura.Filter $filter->addSoftRule('username', $filter::IS, 'alnum'); $filter->addSoftRule('username', $filter::IS, 'strlenBetween', 6, 12); $filter->addSoftRule('username', $filter::FIX, 'string'); $filter->addSoftRule('password', $filter::IS, 'strlenMin', 6); $filter->addSoftRule('password_confirm', $filter::IS, 'equalToField', 'password'); $data = [ 'username' => 'bolivar', 'password' => 'p@55w0rd', 'password_confirm' => 'p@55word', // not the same!]; $success = $filter->values($data); if (! $success) { $messages = $filter->getMessages(); } FOSSMeet 2014 @ NIT Calicut
  21. 21. Validating and Sanitizing ● ● ● ● ● RuleCollection::IS means the field value must match the rule. RuleCollection::IS_NOT means the field value must not match the rule. RuleCollection::IS_BLANK_OR means the field value must either be blank, or match the rule. RuleCollection::FIX to force the field value to comply with the rule; this may forcibly transform the value. RuleCollection::FIX_BLANK_OR will convert blank values to null; non-blank fields will be forced to comply with the rule. FOSSMeet 2014 @ NIT Calicut
  22. 22. Available Rules alnum alpha between blank creditCard dateTime email equalToField equalToValue float inKeys inValues int ipv4 locale max min regex strictEqualToF strictEqualToV ield alue string strlen strlenBetween strlenMax strlenMin trim upload url isbn any all word FOSSMeet 2014 @ NIT Calicut
  23. 23. Custom Rules ● Write a rule class ● Set that class as a service in the RuleLocator ● Use the new rule in our filter chain FOSSMeet 2014 @ NIT Calicut
  24. 24. Rule Class ● ● ● ● ● Extend AuraFilterAbstractRule with two methods: validate() and sanitize() Add params as needed to each method. Each method should return a boolean: true on success, or false on failure. Use getValue() to get the value being validated, and setValue() to change the value being sanitized. Add a property $message to indicate a string that should be translated as a message when validation or sanitizing fails. FOSSMeet 2014 @ NIT Calicut
  25. 25. Example use AuraFilterAbstractRule; class Hex extends AbstractRule { protected $message; public function validate($max = null) { $value = $this->getValue(); } public function sanitize($max = null) { $this->setValue('Hello'); // some code } } $locator = $filter->getRuleLocator(); $locator->set('hex', function () { return new Hex; }); FOSSMeet 2014 @ NIT Calicut
  26. 26. Questions ? ● Hire me to coach you PHP, Aura Hari KT harikt.com https://github.com/harikt FOSSMeet 2014 @ NIT Calicut
  27. 27. more library / framework Aura.Cli Aura.Di Aura.Includer Aura.Input Aura.Web Aura.Di Aura.Dispatcher Aura. Sql Aura.View Aura.Html Aura.Http Aura.Intl Aura.Router Aura.Signal Aura.Filter Aura.Session Aura.Sql_Query Aura.Sql_Schema Aura.Marshal Aura.Autoload Aura.Web_Project Aura.Framework Aura.Framework_Project Aura.Project_Kernel Aura.Web_Kernel Aura.Web_Project Aura.Cli_Kernel Aura.Cli_Project ..... FOSSMeet 2014 @ NIT Calicut
  28. 28. PHP User Group in Calicut ● ● ● Plan to organize a user group meetup once in a month Looking for space and audience Join https://groups.google.com/forum/#! forum/php-clt FOSSMeet 2014 @ NIT Calicut
  29. 29. Thanks ● Website : http://auraphp.com/ ● Source Code : http://github.com/auraphp ● ● ● ● Groups : https://groups.google.com/d/forum/auraphp Paul M Jones : http://paul-m-jones.com/ Chris Tankersley http://www.slideshare.net/ctankersley/dtyu SolarPHP : http://solarphp.com/ FOSSMeet 2014 @ NIT Calicut
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×