11<?php
22
33//GET route
4- $ app ->get ('/ ' , function () use ( $ app ) {
4+ $ app ->get ('/ ' , ' route_default ' );
55
6- $ guests = R::findAll ('guest ' , 'ORDER BY modify_date DESC ' );
7- $ options = array ();
8- $ options ['guests ' ] = $ guests ;
9- $ options ['pmenu ' ] = array (
10- array ('desc ' => 'Slim ' , 'url ' => 'http://www.slimframework.com/ ' ),
11- array ('desc ' => 'Redbean ' , 'url ' => 'http://redbeanphp.com/ ' ),
12- array ('desc ' => 'Twig ' , 'url ' => 'http://twig.sensiolabs.org/ ' ),
13- array ('desc ' => 'Twitter Bootstrap ' , 'url ' => 'http://twitter.github.io/bootstrap/ ' ),
14- );
15- $ options ['smenu ' ] = array (
16- array ('desc ' => 'GitHub Repository ' , 'url ' => 'https://github.com/vanting/RedSlim ' ),
17- array ('desc ' => 'Composer/Packagist ' , 'url ' => 'https://packagist.org/packages/redslim/redslim ' ),
18- array ('desc ' => 'Pagoda Box App Cafe ' , 'url ' => 'https://pagodabox.com/cafe/vanting/redslim ' ),
19- );
20- $ app ->view ()->appendData ($ options );
21- $ app ->render ('demo.html.twig ' );
22- });
6+ function route_default () {
7+ $ app = Slim::getInstance ();
8+ $ guests = R::findAll ('guest ' , 'ORDER BY modify_date DESC ' );
9+ $ options = array ();
10+ $ options ['guests ' ] = $ guests ;
11+ $ options ['pmenu ' ] = array (
12+ array ('desc ' => 'Slim ' , 'url ' => 'http://www.slimframework.com/ ' ),
13+ array ('desc ' => 'Redbean ' , 'url ' => 'http://redbeanphp.com/ ' ),
14+ array ('desc ' => 'Twig ' , 'url ' => 'http://twig.sensiolabs.org/ ' ),
15+ array ('desc ' => 'Twitter Bootstrap ' , 'url ' => 'http://twitter.github.io/bootstrap/ ' ),
16+ );
17+ $ options ['smenu ' ] = array (
18+ array ('desc ' => 'GitHub Repository ' , 'url ' => 'https://github.com/vanting/RedSlim ' ),
19+ array ('desc ' => 'Composer/Packagist ' , 'url ' => 'https://packagist.org/packages/redslim/redslim ' ),
20+ array ('desc ' => 'Pagoda Box App Cafe ' , 'url ' => 'https://pagodabox.com/cafe/vanting/redslim ' ),
21+ );
22+ $ app ->view ()->appendData ($ options );
23+ $ app ->render ('demo.html.twig ' );
24+ }
2325
24- $ app ->get ('/api/comment/json ' , function () use ( $ app ) {
26+ $ app ->get ('/api/comment/json ' , ' api_comment_json ' )-> name ( ' api_comment_json ' );
2527
26- $ result = R::getAll ('SELECT * FROM guest ORDER BY modify_date DESC ' );
27- header ("Content-Type: application/json " );
28- echo json_encode ($ result );
29- })->name ('api_comment_json ' );
28+ function api_comment_json () {
29+ $ app = Slim::getInstance ();
30+ $ result = R::getAll ('SELECT * FROM guest ORDER BY modify_date DESC ' );
31+ header ("Content-Type: application/json " );
32+ echo json_encode ($ result );
33+ }
3034
3135//POST route
32- $ app ->post ('/guest/comment ' , function () use ( $ app ) {
36+ $ app ->post ('/guest/comment ' , ' guest_comment ' )-> name ( ' guest_comment ' );
3337
34- $ guest = R::dispense ('guest ' );
38+ function guest_comment () {
39+ $ app = Slim::getInstance ();
40+ $ guest = R::dispense ('guest ' );
3541
36- $ name = $ app ->request ->post ('name ' );
37- if (empty ($ name ))
38- $ name = 'anonymous ' ;
42+ $ name = $ app ->request () ->post ('name ' );
43+ if (empty ($ name ))
44+ $ name = 'anonymous ' ;
3945
40- $ guest ->name = $ name ;
41- $ guest ->message = $ app ->request ->post ('message ' );
42- $ guest ->ip = $ app ->request ->getIp ();
43-
44- // prepare to delete old comments
45- $ yesterday = date ('Y-m-d ' , strtotime ('-1 day ' ));
46-
47- // start transaction
48- R::begin ();
49- try {
50- R::exec ('DELETE FROM guest WHERE modify_date < ? ' , array ($ yesterday ));
51- R::store ($ guest );
52- R::commit ();
53- $ app ->flash ('success ' , 'Nice to hear from you! ' );
54- } catch (Exception $ e ) {
55- R::rollback ();
56- $ app ->flash ('error ' , 'Oops... seems something goes wrong. ' );
57- }
58- $ app ->redirect ($ app ->request ->getReferrer ());
59- })->name ('guest_comment ' );
46+ $ guest ->name = $ name ;
47+ $ guest ->message = $ app ->request ()->post ('message ' );
48+ $ guest ->ip = $ app ->request ()->getIp ();
6049
61- //PUT route
62- $ app ->put ('/put ' , function () use ($ app ) {
63- echo 'This is a PUT route ' ;
64- });
50+ // prepare to delete old comments
51+ $ yesterday = date ('Y-m-d ' , strtotime ('-1 day ' ));
52+
53+ // start transaction
54+ R::begin ();
55+ try {
56+ R::exec ('DELETE FROM guest WHERE modify_date < ? ' , array ($ yesterday ));
57+ R::store ($ guest );
58+ R::commit ();
59+ $ app ->flash ('success ' , 'Nice to hear from you! ' );
60+ } catch (Exception $ e ) {
61+ R::rollback ();
62+ $ app ->flash ('error ' , 'Oops... seems something goes wrong. ' );
63+ }
64+ $ app ->redirect ($ app ->request ()->getReferrer ());
65+ }
6566
66- //DELETE route
67- $ app ->delete ('/delete ' , function () use ($ app ) {
68- echo 'This is a DELETE route ' ;
69- });
7067?>
0 commit comments