Created
April 12, 2011 18:48
-
-
Save alganet/916119 to your computer and use it in GitHub Desktop.
Another microframework concept
This file contains hidden or 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 | |
$r = new Respect\Rest\Router; | |
$r->get('/', function() { | |
return 'Hello World'; | |
}); | |
$r->get('/users', function() use($users) { | |
return $users->list()->toHTML(); //sample model/view call | |
}); | |
$r->get('/posts/*/*/*'), function($year=null,$month=null,$day=null) use($posts) { | |
return $posts->find($year, $month, $day)->toHTML(); //sample model/view call | |
}); |
This file contains hidden or 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 | |
$r = new Respect\Rest\Router; | |
$singleArticle = $r->get('/articles/*', function($id) use($articles) { | |
return $articles->show($id)->toHTML(); //sample model/view call | |
}); | |
$r->post('/article', function() use ($articles) { | |
$createdId = $articles->create($_POST); //sample model/view call | |
return $singleArticle->redirect($createdId); | |
}); |
This file contains hidden or 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 | |
$r = new Respect\Rest\Router; | |
$view = new Twig_BlaBlaBla; | |
$r->get('/users', function() use($users, $view) { | |
$userList = $users->list(); //sample model call | |
return $view->display('users.phtml', $userList); | |
}); |
This file contains hidden or 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 MyArticle { | |
public function __construct($db) { /* bla bla bla */ } | |
public function post($articleId=null) { /* bla bla bla */ } | |
public function get($articleId) { /* bla bla bla */ } | |
} | |
$r = new Respect\Rest\Router; | |
//bind entire class | |
$r->resource('/articles/*', 'MyArticle', $db); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A idéia é algo 100% REST mesmo! Usando esse conceito, a maioria dos CRUDs completos seria composta de duas ou mais classes. Um blog simples seria descrito assim:
Ao contrário dos controllers normais, REST é naturalmente hierárquico. Então algumas ações acabam se deslocando pra um pai ou filho do controller em questão, como acontece aí acima.