Very small framework for website write in PHP.
- PHP >= 5.4
- URL rewriting so that all requests are handled by
index.php
, you can justphp -S 0.0.0.0:80
Hello World
require 'php-tiny-fram/ptf/autoload.php';
$routers = [
['GET', '%^/$%', function () {
echo 'hello';
}],
];
run($routers);
Named parameters
$routers = [
['GET', '%^/user/(?<name>\d+)$%', function ($params) {
echo 'hello ', $params['name'];
}],
];
echo json
$routers = [
['GET', '%^/api/v2/post/$%', function ($params) {
echo json([1, 2, 3]);
}],
];
You can pass the routers to run()
.
function run($rules, $page404 = null)
$rules
is an array of rule. Each rule has 3 elements:
- http method
- url rule, using regex.
- a function to call when url matches
- (optional) event of before
If none of the rule matched, it will call the function $page404
.
run([
['GET', '%^/$%', $func],
], function() {
echo '404 page'; // already send 404 header
});
All the callbacks of url rule recieve a prameter.
function callback($params)
which $params
is an array, of 3rd parameter of the preg_match()
.
- You can use
%
instead of/
as the deliminator as/
is always in pattern. - Your use a named group such as
%^/topic/(?<topic_id>\d+)$%
, which You can recieve it as association array of callback function.
Register a service:
Service('db', new Pdo('mysql:dbname=xc', 'root', ''));
// later
$db = Service('db');
query data
Service('db')->queryAll('select * from topic where id > ?', [3]);
insert
$id = Service('db')->insert('topic', ['title' => 'bar', 'body' => 'foo']);
update
Service('db')->update('topic', ['title' => 'bar', 'body' => 'foo'], ['id' => 3]);
Every rule has 4th element to specify before event.
run([
['GET', '%^/$%', $func, function() {
if (!is_login()) {
return false; // return false makes the routing stop
}
}],
]);
render('index.html', ['data' => $data]);
Or render with the layout
render('index.html', ['data' => $data], 'layout.html');
where layout.html can render index.html as $_inner_
.
<!-- file: layout.html -->
<html><head></head><body>
<?php include $_inner_; ?>
</body></html>
(MIT License)
Copyright (c) 2015 wangxiaochi cumt.xiaochi@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.