定制 niltonduarte/route 二次开发

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

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

niltonduarte/route

最新稳定版本:v1.0.2

Composer 安装命令:

composer require niltonduarte/route

包简介

A lightweight, simple and easy request router for your application.

README 文档

README

For more details, see the examples folder inside the library. It has no dependencies. Use this with any server, including PHP's built-in server. Inside the folder where you downloaded the component open the terminal and run the command:

php -S localhost:80 -t .

or

php -S localhost:80

If you have a problem, just change the 80 to another port number. For more details on PHP's built-in web server see https://www.php.net/manual/en/features.commandline.webserver.php

Its use is ridiculously easy!

I recommend that you create an index.php file in the root of the public folder that will be accessed by the server when it receives a request from some page of your application and call composer autoload to load Route like this:

<?php
require __DIR__ . '/path/to/vendor/autoload.php';

Start by instantiating the Route\Route\Route class that implements the Route\Route\RouteInterface interface.

$route = new Route\Route\Route();

The Route component supports the five most common HTTP methods in applications, GET, POST, PUT, PATCH and DELETE.

To define a route, call one of the methods of the Route\Route\RouteInterface interface, passing a route and a handle as a parameter:

$route->get('/route-name', 'handler');

The handler can be a method of a class or a callback function.

Passing parameters via URL

Route works with friendly URL and you can pass parameters via URL between curly braces without using querystring. Remembering that you can still use them if you prefer.

$route->get('/route-name/{foo}/{bar}', function (array $params) {
    print_r($params);
    echo '<p>Page content</p>';
});

Respond to other HTTP methods

If the handler is a class with a method, you must write in the second parameter as a string the fully qualified name of the class as recommended by PSR-4, separated from the method by the character @.

$route->get('/route-one', 'VendorNamespace\SubNamespace\ControllerName@methodA');
$route->post('/route-two', 'VendorNamespace\SubNamespace\ControllerName@methodB');
$route->put('/route-three', 'VendorNamespace\SubNamespace\ControllerName@methodC');
$route->patch('/route-four', 'VendorNamespace\SubNamespace\ControllerName@methodD');
$route->delete('/route-five', 'VendorNamespace\SubNamespace\ControllerName@methodE');

dispatching the route

After defining all the routes of your web application, call the run() method which will do the routing based on the accessed URI.

$route->run();

To catch errors, just call the getError() method after run(), which will return an object with the error information. As in the case of a non-existent route.

if ($error = $route->getError()) {
    // Handle errors here.
    $code = $error->getHttpErrorCode();
    $message = $error->getHttpErrorMessage();
    echo "Error {$code}, {$message}!";
}

See the examples folder

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2022-07-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固