gum/gum

将URL映射到回调。

0.3.2 2020-05-20 15:56 UTC

This package is not auto-updated.

Last update: 2024-09-19 12:21:36 UTC


README

Gum 是 PHP 的快速原型设计基础,提供 HTTP 路由和事件系统。配合几个其他 PHP 库,Gum 可以让你 快速原型化应用程序! 哇。

忽略最佳实践

这不是关于纯度,惯例或其他任何事情,只是快速-。所以,你可能不喜欢 Singleton 模式或其他一些东西——请随意提出问题。但请记住,快速-是首要的。

需要 PHP 5.4 或更高版本

PHP 5.4 现在已经可以用于生产环境。这意味着 Gum 也准备好用于生产了吗?不是。

你好...宇宙?

<?php
require 'path_to/Gum.php';
use Gum\Route as Route;
use Gum\Request as Req;
use Gum\Response as Resp;

// The actual URL does not need a trailing slash
Route::get('/', function() {
  echo "Home";
});

// Named route parameters
Route::get('archive/:year/:month/:day', function($year = NULL, $month = NULL, $day = NULL) {
  var_dump($year, $month, $day);
});

// Regex routes, params are passed in an array
Route::get('post/([\d]+)/([a-zA-Z0-9_]+)?', function($args) {
  // if just 'post/' is accessed, $args will be empty array
  var_dump($args);
});

// if '/thing' is accessed through any other request method
// other than 'post', this callback will never be called.
Route::post('thing', function() {
  echo "Thing";
});

// There's also simple helper methods for working with json
Route::post('/json', function() {
  // If you have an ajax form post json to '/json'
  $data = Req::json();
  // You can respond with json easily as well
  echo Res::json(array('success'=>true));
});

// If you need a route to be accessible to both GET and POST
// requests, use `request`. The callback will be fired in
// both cases.
Route::request('foobar', function() {
  echo "Foobar!";
});

// handle 404
if (Route::not_found()) {
  header('HTTP/1.0 404 Not Found');
  echo '404 Not Found';
  exit;
}
?>

文档/指南