เราเตอร์ 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, Post, Put, Patch และ DELETE 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;(ใบอนุญาต MIT)
ลิขสิทธิ์ (c) 2017 Moxxie - https://github.com/moxxie/moxrouter
ได้รับอนุญาตโดยไม่ต้องเสียค่าใช้จ่ายสำหรับบุคคลใด ๆ ที่ได้รับสำเนาซอฟต์แวร์นี้และไฟล์เอกสารที่เกี่ยวข้อง ("ซอฟต์แวร์") เพื่อจัดการในซอฟต์แวร์โดยไม่มีการ จำกัด รวมถึง แต่ไม่ จำกัด เฉพาะสิทธิ์ในการใช้สำเนาดัดแปลงผสานเผยแพร่เผยแพร่
ประกาศลิขสิทธิ์ข้างต้นและประกาศการอนุญาตนี้จะรวมอยู่ในสำเนาทั้งหมดหรือส่วนสำคัญของซอฟต์แวร์
ซอฟต์แวร์มีให้ "ตามสภาพ" โดยไม่มีการรับประกันใด ๆ ไม่ว่าโดยชัดแจ้งหรือโดยนัยรวมถึง แต่ไม่ จำกัด เฉพาะการรับประกันความสามารถในการค้าการออกกำลังกายสำหรับวัตถุประสงค์เฉพาะและการไม่เข้าร่วม ไม่ว่าในกรณีใดผู้เขียนหรือผู้ถือลิขสิทธิ์จะต้องรับผิดชอบต่อการเรียกร้องความเสียหายหรือความรับผิดอื่น ๆ ไม่ว่าจะเป็นการกระทำของสัญญาการละเมิดหรืออื่น ๆ ที่เกิดขึ้นจากหรือเกี่ยวข้องกับซอฟต์แวร์หรือการใช้งานหรือการติดต่ออื่น ๆ ในซอฟต์แวร์