MoxRouter
1.0.0
超级简单快速的PHP路由器
建议您使用作曲家安装moxrouter。
$ composer require moxxie/moxrouter " ^0.2.0 " 创建一个带有以下内容的index.php文件:
<?php
require ' vendor/autoload.php ' ;
$ router = new Moxxie MoxRouter ();
$ router -> get ( ' /{message} ' , function ( $ message ){
echo " Hello " . $ message . " ! " ;
});
$ router -> run ();您可以使用PHP附带的内置服务器进行测试:
$ php -S localhost:8000http:// localhost:8000/世界现在将显示“你好,世界!”。
Moxrouter支持GET,发布,PUT,补丁和删除HTTP请求
// Will only match GET HTTP requests
$ router -> get ( ' /product/{id} ' , function ( $ id ){
// Return product with id = $id
});
// Will only match POST HTTP requests
$ router -> post ( ' /product ' , function (){
// Create new a product
});
// Will only match PUT HTTP requests
$ router -> put ( ' /product/{id} ' , function ( $ id ){
// Update product with id = $id
});
// Will only match PATCH HTTP requests
$ router -> patch ( ' /product/{id} ' , function ( $ id ){
// Apply changes made to product with id = $id
});
// Will only match DELETE HTTP requests
$ router -> delete ( ' /product/{id} ' , function ( $ id ){
// Delete product with id = $id
});
<?php
$ router = new Moxxie MoxRouter ();
// Create an empty container
$ container = [];
// Add a service to the container
$ container [ ' service ' ] = function (){
return new Service ();
};
$ router -> get ( ' / ' , function (){
// Use the new Service
$ service = $ this -> service ();
});
// Run the router with the container
$ router -> run ( $ container );路线前
$ router -> before ( function (){
// This code will be executed before a route has been executed
});路后
$ router -> after ( function (){
// This code will be executed after a route has been executed
});您可以覆盖默认的404处理程序
$ router -> notFound ( function (){
// This code will be executed when a route is not found
});apache .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]nginx
try_files $uri /index.php;(麻省理工学院许可证)
版权(C)2017 Moxxie -https://github.com/moxxie/moxrouter
特此免费授予获得此软件副本和相关文档文件副本(“软件”)的任何人,以无限制处理该软件,包括无限制的使用权,复制,复制,修改,合并,合并,发布,分发,分发,分发,订婚,和/或允许软件的副本,并允许对以下条件提供以下条件,以下是以下条件。
上述版权通知和此许可通知应包含在软件的所有副本或大量部分中。
该软件是“原样”提供的,没有任何形式的明示或暗示保证,包括但不限于适销性,特定目的的适用性和非侵权的保证。在任何情况下,作者或版权持有人都不应对任何索赔,损害赔偿或其他责任责任,无论是在合同,侵权的诉讼中还是其他责任,是由软件,使用或与软件中的使用或其他交易有关的。