-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
about.page
111 lines (99 loc) · 4.23 KB
/
about.page
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
101
102
103
104
105
106
107
108
109
110
111
---
title: Route
description: Custom route files.
icon: 'M9.64,13.4C8.63,12.5 7.34,12.03 6,12V15L2,11L6,7V10C7.67,10 9.3,10.57 10.63,11.59C10.22,12.15 9.89,12.76 9.64,13.4M18,15V12C17.5,12 13.5,12.16 13.05,16.2C14.61,16.75 15.43,18.47 14.88,20.03C14.33,21.59 12.61,22.41 11.05,21.86C9.5,21.3 8.67,19.59 9.22,18.03C9.5,17.17 10.2,16.5 11.05,16.2C11.34,12.61 14.4,9.88 18,10V7L22,11L18,15M13,19A1,1 0 0,0 12,18A1,1 0 0,0 11,19A1,1 0 0,0 12,20A1,1 0 0,0 13,19M11,11.12C11.58,10.46 12.25,9.89 13,9.43V5H16L12,1L8,5H11V11.12Z'
color: '#4a8221'
author: Taufik Nurrohman
type: Markdown
version: 2.0.0
...
Add some PHP files to the `.\lot\route` folder. Each file in this folder serves as an executable function for the
routes:
~~~ .txt
.\
└── lot\
└── route\
├── user\
│ └── create.php
├── contact.php
└── user.php
~~~
A route file contains a function to be executed by the `route` hook. It provides several arguments that you can use in
the function. You can also return a value to be received by the next hook (if any) at the end of the function.
~~~ .php
<?php
return function ($content, $path, $query, $hash) {
// Do something here…
};
~~~
The function will be executed when the current URL path points to an existing route file without the `.php` extension.
Based on the file structure above, you should have this list of active routes which can be visited publicly:
~~~ .txt
http://127.0.0.1/contact
http://127.0.0.1/user
http://127.0.0.1/user/create
~~~
Routes that are more specific will be executed after routes that are more general (their parent routes). It means that
route file `.\lot\route\user.php` will be executed before route file `.\lot\route\user\create.php` when you visit
`http://127.0.0.1/user/create`. The value returned by the route function from file `.\lot\route\user.php` will be passed
to the first function argument of the route function in file `.\lot\route\user\create.php`.
~~~ .php
// From `.\lot\route\user.php`
return function ($content, $path, $query, $hash) {
// The value of variable `$path` will be `'/user'` when you visit `http://127.0.0.1/user`
// The value of variable `$path` will be `'/user/create'` when you visit `http://127.0.0.1/user/create`
// You actually don’t have to create file `.\lot\route\user\create.php`. You can have your own way to process the path after `'/user'`
if ('/user/create' === $path) {
// Do something here…
} else if (0 === strpos($path, '/user/delete/')) {
$name = basename($path);
// Do something with `$name` here…
} else if (0 === strpos($path, '/user/read/')) {
$name = basename($path);
// Do something with `$name` here…
} else if (0 === strpos($path, '/user/update/')) {
$name = basename($path);
// Do something with `$name` here…
} else {
// Do something else here…
}
// This value will be passed to the child routes
return 123;
};
~~~
~~~ .php
// From `.\lot\route\user\create.php`
return function ($content, $path, $query, $hash) {
// The value of variable `$content` will be `123`
// The value of variable `$path` will be `'/user/create'`
}
~~~
Below is a route definition example to read a specific page file stored in a custom folder which you can see the result
by visiting `http://127.0.0.1/a-static-page`:
~~~ .php
// From `.\lot\route\a-static-page.php`
// We can omit the arguments here because they are not needed in the function body
return function () {
// Set some page conditionals which will also effect the `<html>` classes
State::set([
'has' => [
'next' => false,
'page' => true,
'pages' => false,
'part' => false,
'prev' => false
],
'is' => [
'error' => false,
'page' => true,
'pages' => false
]
]);
// Store current page to variable `$page` globally so that it can be shared to the layout
lot('page', $page = new Page('.\path\to\a-static-page.page'));
// Append page title to the current window title
lot('t')[] = $page->title;
// Return a response for layout, so it will load `.\lot\y\*\page.php` file in the end
return ['page', [], 200];
};
~~~