greenswitch / ease-routes
一个易于使用的PHP路由库
dev-main
2022-03-11 06:28 UTC
This package is not auto-updated.
Last update: 2024-10-01 03:37:00 UTC
README
简化的PHP - 路由库
#安装
- 将仓库(下载它)克隆到您的PHP项目文件夹中
- 将htaccess放在您的PHP项目的根目录下
- 完成,准备实施
#实施
- `.htaccess`会自动将您的项目指向`index.php`,您的所有路由逻辑都将放置在这里
- 在`easeroutes/src/Routes.php`中require `Routes.php`文件
- 如果您使用的是composer,请确保在`composer.json`文件中将项目添加如下所示:{"autoload": {"psr-4": {"EaseRoutes": "location-to-this-folder/easeroutes/src"}}}
- 运行dump-autoload
- 您现在可以开始编码了
#COMPOSER "greenswitch/ease-routes": "dev-main"
#在index.php中的代码
---------------使用COMPOSER时---------------- require "vendor/autoload.php";
use EaseRoutes\Routes;
$router = new Routes();
$router->levels = 2;//start trimming url from level 2
$router->rootDir="";/*when deploying to production server, make sure to comment this section above*/
$router->get("","views/homepage.php");
$router->get("/admin","views/homepage.php");
$router->get("/products","views/products.php");
$router->get("/products/:productID/delete","views/product.php");
//dont forget to include this page incase the above routes did not work
$router->load404("views/404.php");
-------------------------------------------------------------
----------------------- PURE PHP-----------------------------
require "easeroutes/src/Routes.php";
$router = new Routes();
$router->levels = 2;//start trimming url from level 2
$router->rootDir="";/*when deploying to production server, make sure to comment this section above*/
$router->get("","views/homepage.php");
$router->get("/admin","views/homepage.php");
$router->get("/products","views/products.php");
$router->get("/products/:productID/delete","views/product.php");
//dont forget to include this page incase the above routes did not work
$router->load404("views/404.php");
#METHODS DEFINITION
Routes::get(route, pagetoload) => bool
- a route definition with get properties.
a. route : a given url can be /products, /login, /admin. Always make sure this url begins with a forward slash
b. pagetoload : a physical file to be loaded relative to the root directory
Routes::post(route,pagetoload) => bool
- same as get but with post
Routes::load404("path/to/404page")
- if either of the given routes are not suplied in the url, then this page will help you relocate the user to this
default not found route. can be an html or php. To access the right error, use $errorCode and $message to display
the message on the UI
Routes::levels (default = 0)
- number of directories to skip to reach the real root directory. For example, you might have your project
located 3 or 5 levels from the root directory in your development folder eg. localhost/projects/socialmedia/
this example has 2 levels to the root directory, projects and socialmedia, so for this library to work properly
in such scenarios, declare the levels property to the number of levels to skip in your directory
Routes::rootDir (default = "")
- Given the example in levels, you also need to tell the library where your project is located if you have a multi
level project. eg the above will be Routes::rootDir = "projects/socialmedia/". This will help in identifying
proper directories to render your php files.