定制 pierre/router 二次开发

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

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

pierre/router

最新稳定版本:1.1.3

Composer 安装命令:

composer require pierre/router

包简介

A router package for your project

README 文档

README

The Router PHP package provides an easy-to-use routing system for PHP web applications. It allows developers to define routes for different HTTP methods such as GET, POST, DELETE, and PUT. Additionally, it supports the use of hidden input fields to simulate the DELETE, PUT, and PATCH HTTP methods.

Requirements

The Router PHP package requires PHP 7.0 or higher.

features

  • Define routes for GET, POST, DELETE, and PUT HTTP methods
  • Define routes with parameters

Installation

To use the Router PHP package, you need to install it via Composer. Run the following command in your project directory:

composer require pierre/router

Usage

To use the Router, you need to create a new instance of the Router class:

// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';

// Create Router instance
$router = new \Pierre\Router\Router();

// Define routes
// ...

// Run it!

Defining Routes

The Router supports four HTTP methods: GET, POST, DELETE, and PUT. You can define a route for each method using the following methods:

GET

To define a GET route, use the get() method. The first parameter is the URL path, and the second parameter is a callback function that will be executed when the route is accessed:

$router->get('/', function() {
    return 'Hello World!';
});

POST

To define a POST route, use the post() method.

$router->post('/', function() {
    return $_POST['name'];
});

DELETE

To define a DELETE route, use the delete() method.

$router->delete('/user/:id', function($id) {
    return 'User ' . $id . ' deleted';
});

PUT

To define a PUT route, use the put() method.

$router->put('/user/:id', function($id) {
    return 'User ' . $id . ' updated';
});

Executing the Router

After defining your routes, you need to execute the Router using the run() method:

$router->run();

Simulating DELETE, PUT, and PATCH Methods

To simulate the DELETE, PUT, and PATCH HTTP methods in a form, you need to include a hidden input field with the name _method and the value of the desired method. The Router will detect this input field and use the specified method for the request.

Delete example

<form action="/user/delete/1" method="POST">
    <input type="hidden" name="_method" value="DELETE">
    <input type="hidden" name="id" value="1">
    <input type="submit" value="Delete">
</form>

Put example

<form action="/user/edit/1" method="POST">
    <input type="hidden" name="_method" value="PUT">
    <input type="hidden" name="id" value="1">
    <input type="submit" value="Delete">
</form>

Passing Parameters

You can pass parameters to your routes using the following syntax:

$name = "Pierre";
$router->get('/hello', function() use ($name) {
    return "Hello {$name}!}";
});

License

The Router PHP package is open-sourced software licensed under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-27