Add some PHP files to the .\lot\route
folder. Each file in this folder serves as an executable function for the routes:
.\
└── 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
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:
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
.
// 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;
};
// 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
:
// 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
$GLOBALS['page'] = $page = new Page('.\path\to\a-static-page.page');
// Append page title to the current window title
$GLOBALS['t'][] = $page->title;
// Return a response for layout, so it will load `.\lot\y\*\page.php` file in the end
return ['page', [], 200];
};
0 Comments
No comments yet.