ã¾ãã¯ã©ã¹ã¯ä½¿ããªãããªããªãå¿ è¦ãªãããããã£ããã¢ã¸ã¥ã©ã¼ãªé¢æ°ããããããããããªãããæå³ããªããã¯ã©ã¹ã¯ã¬ã¢ã±ã¼ã¹ãé¤ãã¦åºæ¬çã«ã¯æªã
ã¢ãã«ã¯Readç³»ã®APIã¨Writeç³»ã®APIã§åå空éãåããã
<?php /* Articles.php */ namespace Articles\Write; function mkArticle ($title, $text, $tags) { /*ããªãã¼ã·ã§ã³ã¨ããã¦é£æ³é åãä½ã£ã¦è¿ã*/ } function addArticle ($article) {/*DBã«ä¿å*/} namespace Articles\Read; function getArticles ($filter=null) {/*DBããèªã¿åºã*/} function getArticle ($id) {}
ãã¥ã¼ã¯ã¢ãã«ã®Readç³»APIãç´æ¥è¦ããã³ã³ããã¼ã©ãã¢ãã«ãèªãã§ãã¥ã¼ã«å¤ã渡ãå¾æ¥ã®ããããã¯é ããããã
ããã§renderArticleã¯é¢åã ããã¤ã³ã©ã¤ã³ã§htmlæ¸ãã¦ããã©å®éã¯ãã³ãã¬ã¼ãã®å¼ã³åºãã¨ãã«ãªãã
<?php /* ArticlesView.php */ namespace ArticlesView; use Articles\Read as Articles; function renderArticles () { $articles = Articles\getArticles(); array_map('renderArticle', $articles); } function renderArticle ($article) { ?> <div class="article"> <h2><?= $a["title"] ?></h2> <p><?= $a["text"] ?></p> </div> <? } function renderArticleOfId ($id) { renderArticle(Articles\getArticle($id)); }
ã³ã³ããã¼ã©ã¯ã©ã¦ãã£ã³ã°ã©ã¤ãã©ãªã使ã£ã¦æ¸ããã¢ãã«ã®Writeç³»APIã¨ãã¥ã¼ã®APIã«ã¢ã¯ã»ã¹ã§ããã
<?php /* index.php */ namespace App; use Articles\Write as Articles; use ArticleView; use Routy\Router; $app = new Router(); $app->post("articles", function () { Articles\addArticle(Articles\mkArticle($_POST["title"], $_POST["text"], $_POST["tags"])); redirect("/articles"); }); $app->get("articles", function () { ArticleView\renderArticles(); }); $app->get("articles/{alnum}", function ($id) { ArticleView\renderArticleOfId($id); }); $app->run();