承接 basecode/route 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

basecode/route

Composer 安装命令:

composer require basecode/route

包简介

Component route, componente para criação de projetos mvc com rotas.

README 文档

README

route license GitHub tag (latest by date)

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 basecode/route 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 24
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-07-24