thomasmrln / superway
新一代Php路由:简单快速!
1.0.1
2014-04-09 09:11 UTC
Requires
- php: >=5.4.0
This package is not auto-updated.
Last update: 2024-09-28 16:09:30 UTC
README
Superway 是一个 PHP 路由,允许你忘记 .htaccess 以及其他路由 URL 的方法。
易于“安装”和启动。Superway 的创建是为了简化网络开发者的工作。
用法
实例化
<?php // Instantiate your Superway router with your templates directory $sw = new Superway('./templates'); // Superway has require to know the extension of file used $sw->extension = 'php'; // Others with tpl, html, and more
添加路由
通过添加路由来创建你的路由。
路由由以下内容组成:* 文件的路径 * URL 的模式
Superway 的强大之处在于无限地将变量集成到 URL 中,并在模板中使用这些变量的名称获取这些变量。
<?php $sw->add_road( new Road('/home', '/')); $sw->add_road( new Road('/blog/blog', '/blog/')); $sw->add_road( new Road('/blog/blog', '/blog/search/{query}')); $sw->add_road( new Road('/blog/blog', '/blog/category/{id}')); $sw->add_road( new Road('/blog/article', '/blog/article/{date}/{id}_{title}'));
添加非路由
在 Superway 找不到路由的情况下,它会将你带回到所需的路由非路由路径。
<?php $sw->offroad( new Road('/404', '/404'));
使用变量
要使用传递给模式的变量,只需用其名称调用它们即可。
例如,模式 '/blog/article',在这个 URL 下:'http://your_url.com/blog/article/2014-04/112_title-of-your-article/',将使用 $date (== 2014-04)、$id (== 112) 和 $title (==title-of-your-article)
掌握方向盘,开始驾驶!
要驾驶你的路由,只需运行此代码并等待结果...
<?php try { print $sw->drive(); } catch (\Exception $e) { print "Erreur : ".$e->getMessage(); }