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); |
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:
class MyBlog {
public function post(); //novo artigo
public function get(); //listar artigos
}
class MyArticle {
public function get($id); //exibir artigo e comentários
public function delete($id); //excluir artigo
public function put($id); //atualizar artigo
public function post($id); //novo comentário no artigo
}
class MyComment {
public function delete($postId, $commentId); //excluir comentário
public function put($postId, $commentId); //atualizar comentário
}
$r->resource('/articles/', 'MyBlog');
$r->resource('/articles/*', 'MyArticle');
$r->resource('/articles/*/comments/*', 'MyComment');
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A idéia de fazer o bind de uma classe inteira é ótima! A intenção é que o framework seja 100% REST? Ou pretende deixar o usuário mapear uma parte da URL p/ o método da classe também? (esquema ZF)