-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a special update for backward support of PHP 5.2 (remove PSR-0 autolo…
…ader, __DIR__, namespace, closure)
- Loading branch information
Showing
6 changed files
with
103 additions
and
87 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,67 @@ | ||
<?php | ||
|
||
//GET route | ||
$app->get('/', function () use ($app) { | ||
$app->get('/', 'route_default'); | ||
|
||
$guests = R::findAll('guest', 'ORDER BY modify_date DESC'); | ||
$options = array(); | ||
$options['guests'] = $guests; | ||
$options['pmenu'] = array( | ||
array('desc' => 'Slim', 'url' => 'http://www.slimframework.com/'), | ||
array('desc' => 'Redbean', 'url' => 'http://redbeanphp.com/'), | ||
array('desc' => 'Twig', 'url' => 'http://twig.sensiolabs.org/'), | ||
array('desc' => 'Twitter Bootstrap', 'url' => 'http://twitter.github.io/bootstrap/'), | ||
); | ||
$options['smenu'] = array( | ||
array('desc' => 'GitHub Repository', 'url' => 'https://github.com/vanting/RedSlim'), | ||
array('desc' => 'Composer/Packagist', 'url' => 'https://packagist.org/packages/redslim/redslim'), | ||
array('desc' => 'Pagoda Box App Cafe', 'url' => 'https://pagodabox.com/cafe/vanting/redslim'), | ||
); | ||
$app->view()->appendData($options); | ||
$app->render('demo.html.twig'); | ||
}); | ||
function route_default() { | ||
$app = Slim::getInstance(); | ||
$guests = R::findAll('guest', 'ORDER BY modify_date DESC'); | ||
$options = array(); | ||
$options['guests'] = $guests; | ||
$options['pmenu'] = array( | ||
array('desc' => 'Slim', 'url' => 'http://www.slimframework.com/'), | ||
array('desc' => 'Redbean', 'url' => 'http://redbeanphp.com/'), | ||
array('desc' => 'Twig', 'url' => 'http://twig.sensiolabs.org/'), | ||
array('desc' => 'Twitter Bootstrap', 'url' => 'http://twitter.github.io/bootstrap/'), | ||
); | ||
$options['smenu'] = array( | ||
array('desc' => 'GitHub Repository', 'url' => 'https://github.com/vanting/RedSlim'), | ||
array('desc' => 'Composer/Packagist', 'url' => 'https://packagist.org/packages/redslim/redslim'), | ||
array('desc' => 'Pagoda Box App Cafe', 'url' => 'https://pagodabox.com/cafe/vanting/redslim'), | ||
); | ||
$app->view()->appendData($options); | ||
$app->render('demo.html.twig'); | ||
} | ||
|
||
$app->get('/api/comment/json', function () use ($app) { | ||
$app->get('/api/comment/json', 'api_comment_json')->name('api_comment_json'); | ||
|
||
$result = R::getAll('SELECT * FROM guest ORDER BY modify_date DESC'); | ||
header("Content-Type: application/json"); | ||
echo json_encode($result); | ||
})->name('api_comment_json'); | ||
function api_comment_json() { | ||
$app = Slim::getInstance(); | ||
$result = R::getAll('SELECT * FROM guest ORDER BY modify_date DESC'); | ||
header("Content-Type: application/json"); | ||
echo json_encode($result); | ||
} | ||
|
||
//POST route | ||
$app->post('/guest/comment', function () use($app) { | ||
$app->post('/guest/comment', 'guest_comment')->name('guest_comment'); | ||
|
||
$guest = R::dispense('guest'); | ||
function guest_comment() { | ||
$app = Slim::getInstance(); | ||
$guest = R::dispense('guest'); | ||
|
||
$name = $app->request->post('name'); | ||
if (empty($name)) | ||
$name = 'anonymous'; | ||
$name = $app->request()->post('name'); | ||
if (empty($name)) | ||
$name = 'anonymous'; | ||
|
||
$guest->name = $name; | ||
$guest->message = $app->request->post('message'); | ||
$guest->ip = $app->request->getIp(); | ||
|
||
// prepare to delete old comments | ||
$yesterday = date('Y-m-d' , strtotime('-1 day')); | ||
|
||
// start transaction | ||
R::begin(); | ||
try { | ||
R::exec('DELETE FROM guest WHERE modify_date < ?', array($yesterday)); | ||
R::store($guest); | ||
R::commit(); | ||
$app->flash('success', 'Nice to hear from you!'); | ||
} catch (Exception $e) { | ||
R::rollback(); | ||
$app->flash('error', 'Oops... seems something goes wrong.'); | ||
} | ||
$app->redirect($app->request->getReferrer()); | ||
})->name('guest_comment'); | ||
$guest->name = $name; | ||
$guest->message = $app->request()->post('message'); | ||
$guest->ip = $app->request()->getIp(); | ||
|
||
//PUT route | ||
$app->put('/put', function () use($app) { | ||
echo 'This is a PUT route'; | ||
}); | ||
// prepare to delete old comments | ||
$yesterday = date('Y-m-d', strtotime('-1 day')); | ||
|
||
// start transaction | ||
R::begin(); | ||
try { | ||
R::exec('DELETE FROM guest WHERE modify_date < ?', array($yesterday)); | ||
R::store($guest); | ||
R::commit(); | ||
$app->flash('success', 'Nice to hear from you!'); | ||
} catch (Exception $e) { | ||
R::rollback(); | ||
$app->flash('error', 'Oops... seems something goes wrong.'); | ||
} | ||
$app->redirect($app->request()->getReferrer()); | ||
} | ||
|
||
//DELETE route | ||
$app->delete('/delete', function () use($app) { | ||
echo 'This is a DELETE route'; | ||
}); | ||
?> |
This file contains 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
This file contains 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
This file contains 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