定制 kyrill/php-route 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

kyrill/php-route

最新稳定版本:V1.0.2

Composer 安装命令:

composer require kyrill/php-route

包简介

A router package in development

README 文档

README

The PHP-route is a lightweight PHP library that simplifies routing in web applications. It allows you to define routes and associate them with controllers, functions, or anonymous functions for handling HTTP requests.

Installation

You can easily install the PHP Routing System using Composer:

   composer require "kyrill/php-route"

Usage

Basic setup

In your index.php file, the following code is required for the package to work:

<?php
require 'vendor/autoload.php';

In the file where you want to use the routing

To utilize the file for route definitions, incorporate the following code into your routing file:

<?php
use Kyrill\PhpRoute\Router;
$router = new Router(); // add an instance of the Router

$router->resolveRoute();// resolves all the requests

The resolveRoute() method will return true if a route is found and false if no route is found. You can use this to display a 404 page if no route is found:

Basic usage

You can add routes to a controller like this:

$router->addRoute('GET', '/home', [Controller::class, 'home']);

You can also use functions as route handlers:

$router->addRoute('GET','/routename', 'nameFunction')
});

public function nameFunction(){
    echo 'Hello function!';
}

Additionally, anonymous functions can be used as route handlers:

$router->addRoute('GET','/anonymousfunction', function () {
    echo 'Hello anonymous function!';
});

In these examples, we use the GET method, but you can use any HTTP method you need for your routes.

You can also use parameters in your routes, if you don't specify an expression the default is ([0-9]+):

$router->addRoute('GET','/user/{id}', [Controller::class, 'home']);

You also can use regular expressions in your routes:

$router->addRoute('GET','/user/{id:[0-9]+}', [Controller::class, 'home']);

Currently only class methods are supported for parameterized routes.

Middleware

You can add middlewares to the addRoute function. Middleware will execute before the controller or (anonymous) function. Your middleware class needs to implement the MiddlewareInterface. Your class needs to have the handle function; this is the function that is called by the router package. When you have done this, you can add the middleware to your function like this:

$router->addRoute('GET', '/home', [Controller::class, 'home'], [MiddlewareClass::class]);

license

This project is licensed under the MIT License - see the LICENSE.md file for details

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-26