This repository was archived by the owner on Nov 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.php
More file actions
69 lines (57 loc) · 1.72 KB
/
index.php
File metadata and controls
69 lines (57 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
require 'Slim/Slim.php';
require 'vendor/ActiveRecord.php';
require 'Views/TwigView.php';
// initialize ActiveRecord
// change the connection settings to whatever is appropriate for your mysql server
ActiveRecord\Config::initialize(function($cfg)
{
$cfg->set_model_directory('models');
$cfg->set_connections(array(
'development' => 'mysql://serverside:password@127.0.0.1/slimactiverecord'
));
});
// Configure Twig
TwigView::$twigDirectory = dirname(__FILE__) . '/vendor/Twig';
// Adding in extension
TwigView::$twigExtensions = array(
'Extension_Twig_Slim'
);
// Initialize Slim with TwigView
$app = new Slim(array(
'view' => 'TwigView'
));
$app->get('/', function () use ($app) {
$data['tasks'] = Task::find('all');
$app->render('task/index.html', $data);
})->name('tasks');
$app->post('/task/new/', function () use ($app) {
$task = new Task();
$task->name = "My New Task";
$task->done = 0;
$task->save();
if($task->id > 0)
{
$app->redirect($app->urlFor('tasks'));
}
})->name('task_new');
$app->get('/task/:id/edit', function ($id) use ($app) {
$data['task'] = Task::find($id);
$app->render('task/edit.html', $data);
})->name('task_edit');
$app->post('/task/:id/edit', function ($id) use ($app) {
$task = Task::find($id);
$task->name = $app->request()->post('name');
$task->done = $app->request()->post('done') === '1' ? 1 : 0;
$task->save();
if($task->id > 0)
{
$app->redirect($app->urlFor('tasks'));
}
})->name('task_edit_post');
$app->get('/task/:id/delete', function ($id) use ($app) {
$task = Task::find($id);
$task->delete();
$app->redirect($app->urlFor('tasks'));
})->name('task_delete');
$app->run();