popphp / popcorn
Popcorn,一个基于REST的PHP微框架
Requires
- php: >=8.1.0
- popphp/pop-cookie: ^4.0.1
- popphp/pop-http: ^5.1.0
- popphp/pop-session: ^4.0.0
- popphp/pop-view: ^4.0.0
- popphp/popphp: ^4.2.9
Requires (Dev)
- phpunit/phpunit: ^10.0.0
Suggests
- popphp/pop-db: For DB management and transactions
- popphp/pop-form: For HTML form generation and validation
README
版本信息
Popcorn PHP REST-based Micro Framework 4.1.1
发布日期:2023年10月16日
概述
Popcorn PHP Micro Framework是一个基于REST的微框架。它是一个小型的组件,作为Pop PHP的一层,以强制执行Web应用的REST基于路由规则。它支持PHP 8.1以上。
popcorn
是Pop PHP Framework的组件。
安装
使用Composer安装popcorn
。
composer require popphp/popcorn
或者,在您的composer.json文件中要求它
"require": {
"popphp/popcorn" : "^4.1.1"
}
快速入门
在一个简单的index.php
文件中,您可以定义您希望在应用程序中允许的路由。在这个例子中,闭包用作控制器。通配符路由*
可以用作“通配符”来处理未找到或未允许的路由。
use Popcorn\Pop; $app = new Pop(); // Home page: GET https:/// $app->get('/', function() { echo 'Hello World!'; }); // Say hello page: GET https:///hello/world $app->get('/hello/:name', function($name) { echo 'Hello ' . ucfirst($name) . '!'; }); // Wildcard route to handle errors $app->get('*', function() { header('HTTP/1.1 404 Not Found'); echo 'Page Not Found.'; });
上面的例子定义了两个GET
路由和一个通配符来处理失败。
我们可以定义一个如下的POST
路由
// Post auth route: POST https:///auth $app->post('/auth', function() { if ($_SERVER['HTTP_AUTHORIZATION'] == 'my-token') { echo 'Auth successful'; } else { echo 'Auth failed'; } }); $app->run();
如果您尝试通过GET(或任何非POST方法)访问上述URL,则会失败。如果您通过POST访问该URL,但带有错误的令牌,它将返回由应用程序强制执行的认证失败
消息。使用正确的令牌通过POST访问URL,则操作将成功。
$ curl -X POST --header "Authorization: bad-token" https:///auth
Auth failed
$ curl -X POST --header "Authorization: my-token" https:///auth
Auth successful
高级
在一个更高级的例子中,我们可以利用更多MVC风格的配置,使用Pop PHP的核心组件和Popcorn连接应用程序。保持简单,让我们看看控制器类MyApp\Controller\IndexController
如下所示
<?php namespace MyApp\Controller; use Pop\Controller\AbstractController; use Pop\Http\Server\Request; use Pop\Http\Server\Response; use Pop\View\View; class IndexController extends AbstractController { protected Request $request; protected Response $response; protected string $viewPath; public function __construct( Request $request = new Request(), Response $response = new Response() ): void { $this->request = $request; $this->response = $response; $this->viewPath = __DIR__ . '/../view/'; } public function index(): void { $view = new View($this->viewPath . '/index.phtml'); $view->title = 'Hello'; $this->response->setBody($view->render()); $this->response->send(); } public function hello($name): void { $view = new View($this->viewPath . '/index.phtml'); $view->title = 'Hello ' . $name; $this->response->setBody($view->render()); $this->response->send(); } public function error(): void { $view = new View($this->viewPath . '/error.phtml'); $view->title = 'Error'; $this->response->setBody($view->render()); $this->response->send(404); } }
以及两个视图脚本,分别是index.phtml
和error.phtml
<!DOCTYPE html> <!-- index.phtml //--> <html> <head> <title><?=$title; ?></title> </head> <body> <h1><?=$title; ?></h1> </body> </html>
<!DOCTYPE html> <!-- error.phtml //--> <html> <head> <title><?=$title; ?></title> </head> <body> <h1 style="color: #f00;"><?=$title; ?></h1> <p>Sorry, that page was not found.</p> </body> </html>
然后我们可以设置应用程序如下
use Popcorn\Pop; $app = new Pop(); $app->get('/', [ 'controller' => 'MyApp\Controller\IndexController', 'action' => 'index', 'default' => true ])->get('/hello/:name', [ 'controller' => 'MyApp\Controller\IndexController', 'action' => 'hello' ]); $app->run();
default
参数将控制器设置为默认控制器,用于处理未找到的路由。通常,控制器中有一个默认操作,如error
方法,用于处理此操作。
自定义方法
如果您的Web服务器允许配置自定义HTTP方法,Popcorn支持这一点,并允许您将自定义HTTP方法注册到应用程序中。
use Popcorn\Pop; $app = new Pop(); $app->addCustomMethod('PURGE') ->addCustomMethod('COPY'); $app->purge('/image/:id', function(){ // Do something with the PURGE method on the image URL }); $app->copy('/image/:id', function(){ // Do something with the COPY method on the image URL }); $app->run();
然后您可以像这样提交带有自定义HTTP方法的请求
$ curl -X PURGE https:///image/1
$ curl -X COPY https:///image/1