承接 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

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固