-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathindex.php
More file actions
100 lines (88 loc) · 2.61 KB
/
index.php
File metadata and controls
100 lines (88 loc) · 2.61 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* Step 1: Require the Leaf Framework
*
* If you are not using Composer, you need to require the
* Leaf Framework and register its PSR-0 autoloader.
*
* If you are using Composer, you can skip this step.
*/
require 'Leaf/App.php';
/**
* Composer autoloader for extra packages
*/
require 'vendor/autoload.php';
\Leaf\App::registerAutoloader();
/**
* Step 2: Instantiate a Leaf application
*
* This example instantiates a Leaf application using
* its default settings. However, you will usually configure
* your Leaf application now by passing an associative array
* of setting names and values into the application constructor.
*/
$app = new \Leaf\App();
/**
* Initialise the Leaf Auth package
*/
$auth = new \Leaf\Auth();
/**
* Leaf's 404 Handler, you can pass in a custom HTML page or text as a function
*/
$app->set404();
/**
* Step 3: Define the Leaf application routes
*
* Here we define several Leaf application routes that respond
* to appropriate HTTP request methods. In this example, the second
* argument for `Leaf::get`, `Leaf::post`, `Leaf::put`, `Leaf::patch`, and `Leaf::delete`
* is an anonymous function.
*/
// Home Route with Blade Templating
$app->get('/', function () use($app) {
$app->blade->configure("app/pages", "app/pages/cache");
$page = $app->blade->render("index", [
"title" => "Leaf PHP Framework",
"welcome" => 'Congratulations, you\'re on <span class="green">Leaf</span>'
]);
$app->response->renderMarkup($page);
});
// POST route
$app->post('/post', function () use ($app) {
$app->response->respond($app->request->body());
});
// Example User Login
$app->get("/login", function() use($app, $auth) {
// connect to the database
$auth->connect("localhost", "root", "", "test");
// sign a user in, in literally 1 line
// $user = $auth->login("users", ["username" => "mychi", "password" => "test"], "md5");
$user = $auth->register("users", [
"username" => "sally",
"email" => "sally@gmail.com",
"password" => "test"
], ["username", "email"], "md5");
// return json encoded data
$app->response->respond(
!$user ? $auth->errors() : $user
);
});
// PUT route
$app->put( '/put', function () {
echo 'This is a PUT route';
});
// PATCH route
$app->patch('/patch', function () {
echo 'This is a PATCH route';
});
// DELETE route
$app->delete('/delete', function () {
echo 'This is a DELETE route';
});
/**
* Step 4: Run the Leaf application
*
* This method should be called last. This executes the Leaf application
* and returns the HTTP response to the HTTP client.
*/
$app->run();