如果您在链接之前没有使用过这个:http: //altorouter.com/
我正在制作一个小型应用程序,但不需要框架,只需要路由部分。所以我决定尝试 altorouter,因为它看起来很简单。
我想映射某些路线来做某些事情。例如:
www.example.com/products/
这应该显示我的 products.php 模板并从数据库中提取数据以填充字段。我有这个工作与以下:
$router->map( 'GET', '/products', function() {
require('partials/connectdb.php'); //Require Database Connection
$pageContent = getProductsContent($conn); //prepare all the page content from database
require __DIR__ . '/products.php'; //require the template to use
});
所有其他标准页面都一样,我的问题是路线何时可以更改。例如:
www.example.com/shoes/
www.example.com/shorts/
www.example.com/shirts/
www.example.com/ties/
当用户转到这些路线时,我想获取参数“鞋子”,然后在仍然使用 products.php 模板的同时,只为鞋子执行逻辑。
所以看看它说你可以做的文档:
www.example.com/[*] //这意味着什么。
但是,在将其添加到我列出的路线后,它会使用户尝试访问的任何其他内容无效。因此,如果他们访问:
www.example.com/products // 就像以前一样
它实际上做了里面的逻辑:
www.example.com/[*]
有谁知道 altorouter 可以帮助我吗?我将在下面粘贴我的完整页面代码:
// Site Router
$router->map( 'GET', '/', function() {
require('partials/connectdb.php'); //Require Database Connection
$pageContent = getHomeContent($conn);
require __DIR__ . '/home.php';
});
$router->map( 'GET', '/products', function() {
require('partials/connectdb.php'); //Require Database Connection
$pageContent = getProductsContent($conn);
require __DIR__ . '/products.php';
});
$router->map( 'GET', '/[*]', function($id) {
require('partials/connectdb.php'); //Require Database Connection
$test = 'this was a test';
$pageContent = getProductsContent($conn);
require __DIR__ . '/products.php';
});
// match current request url
$match = $router->match();
// call closure or throw 404 status
if( $match && is_callable( $match['target'] ) ) {
call_user_func_array( $match['target'], $match['params'] );
} else {
// no route was matched
header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
}