basecode/route
Composer 安装命令:
composer require basecode/route
包简介
Component route, componente para criação de projetos mvc com rotas.
README 文档
README
What's the route ?
Summary: Route is a standalone component for creating routes in mvc systems.
Features
- Creating quick routes.
- Creating routes with dynamic parameters.
- Creating routes of type [ get | post | put | patch | delete ].
- Default route creation.
Getting Started
Installation
You can install the route in your project with composer.
Just run the command below on your terminal:
composer require basecode/route
or in your composer.json require:
"basecode/route": "2.1.*"
Usage
After installing the route, it is very easy to use it, just instantiate a route object in your project's index and start creating your routes.
Recommendation for using the route
The recommendation is to have a .htaccess file that points the URI as parameter GET[route] to the index of your project, where the route will be.
Example .htaccess file:
RewriteEngine On Options -Indexes RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?route=$1
or you can pass the variable that receives the route as a parameter to the method execute, see the example below.
Example index.php file:
define("DS", DIRECTORY_SEPARATOR); define("DOMAIN", "https://localhost/project"); require_once dirname(__DIR__).DS."vendor".DS."autoload.php"; use BaseCode\Route\Route; /* domain_url [string] [required] separator_controller_method [string] [optional] [default] = ":" $route = new Route(domain_url, separator_controller_method); # EXAMPLE $route = new Route(DOMAIN, ":"); */ $route = new Route(DOMAIN); /* route [string] [required] route_action [string|function] [required] route_name [string] [optional] $route->get(route, route_action, route_name); $route->post(route, route_action, route_name); $route->put(route, route_action, route_name); $route->delete(route, route_action, route_name); action [string|function] $route->standard(action); # EXAMPLE $route->get("admin/login", function() { echo "<h1>ADMIN LOGIN</h1>"; }, "admin.login"); */ $route->get("/", function() { echo "<h1>HOME PAGE</h1>"; }, "home"); $route->namespace("Controllers\\Admin")->group("admin"); $route->get("/", "Controller:login", "admin.login"); // get route to namespace: Controllers\Admin\Controller - method: login $route->execute(); /* or $route->execute("route"); // route is the name received by $_GET */
Creating standard route
The standard route is used when the route requested via the url is not found.
Example standard route:
$route = new Route(DOMAIN); $route->get("/", function() { echo "<h1>HOME PAGE</h1>"; }, "home"); $route->standard(function($error) { http_response_code($error["code"]); echo "<h1>ERROR: ".$error["message"]."</h1>"; }); $route->execute();
Using the route method
The route method returns the route with the specified name or null if it is not found.
Example route method:
$route = new Route(DOMAIN); $route->get("/", function() use ($route) { $link = $route->route("landing.page"); echo "<h1>HOME PAGE</h1><br><a href=\"{$link}\">Landing</a>"; }, "home"); $route->get("landing", function() use ($route) { $link = $route->route("home"); echo "<h1>LANDING PAGE</h1><br><a href=\"{$link}\">Back Home</a>"; }, "landing.page"); $route->execute();
Passing parameters to the route
It is possible to define parameters for the routes, thus allowing to receive dynamic values.
Example parameters for route:
$route = new Route(DOMAIN); $route->get("hello/{name}", function($data) { $name = $data["name"]; echo "<h1>HELLO {$name}</h1>"; }, "hello.page"); $route->execute();
Get url of current route
It is possible to get the url of the current route using the (current) method
Example of current route:
$route = new Route(DOMAIN); $route->get("product/{id}", function() use ($route) { $current = $route->current(); echo "<p>ROUTE: {$current}</p>"; }, "product.page"); $route->execute();
Route redirection
It is possible to redirect routes using the (redirect) method.
Example of redirect :
$route = new Route(DOMAIN); // REDIRECT OPTIONS /* USING NAME ROUTES EXAMPLES ( $route->get("/home", route_action, "page.home"); $route->get("/product/{id}", route_action, "page.product"); ) $route->redirect("page.home"); // redirect to domain/home $route->redirect("page.product", ["id" => 10]); // redirect to domain/product/10 */ /* USING URL $route->redirect("https://www.google.com"); // redirect to passed URL */ $route->group("admin"); $route->get("/login", function() { echo "<h1>ADMIN LOGIN</h1>"; }, "admin.login"); $route->get("/home", function() use ($route) { if (!isset($_SESSION["ADMIN_USER"])) { $route->redirect("admin.login"); } echo "<h1>ADMIN HOME</h1>"; }, "admin.home"); $route->execute();
To be continued...
basecode/route 适用场景与选型建议
basecode/route 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 07 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「route」 「BaseCode」 「Thcoder」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 basecode/route 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 basecode/route 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 basecode/route 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provide a way to secure accesses to all routes of an symfony application.
Write down your routing mapping at one place
marmot framework basecode
A Laravel-like router for the WordPress Rewrite API
A Laravel helper to detect if the current route/path is active.
A package to easily make use of TypeId with PHP.
统计信息
- 总下载量: 24
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-07-24