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
特此免費授予獲得此軟件副本和相關文檔文件副本(“軟件”)的任何人,以無限制處理該軟件,包括無限制的使用權,複製,複製,修改,合併,合併,發布,分發,分發,分發,訂婚,和/或允許軟件的副本,並允許對以下條件提供以下條件,以下是以下條件。
上述版權通知和此許可通知應包含在軟件的所有副本或大量部分中。
該軟件是“原樣”提供的,沒有任何形式的明示或暗示保證,包括但不限於適銷性,特定目的的適用性和非侵權的保證。在任何情況下,作者或版權持有人都不應對任何索賠,損害賠償或其他責任責任,無論是在合同,侵權的訴訟中還是其他責任,是由軟件,使用或與軟件中的使用或其他交易有關的。