popphp/popcorn
Composer 安装命令:
composer require popphp/popcorn
包简介
Popcorn, A REST-Based PHP Micro Framework
README 文档
README
RELEASE INFORMATION
Popcorn PHP REST-Based Micro Framework 4.1.2
Released December 2, 2024
Overview
Popcorn PHP Micro Framework is a REST-based micro framework. It is a small component that acts as a layer for Pop PHP to enforce the REST-based routing rules of a web application. It supports PHP 8.2+.
popcorn is a component of Pop PHP Framework.
Install
Install popcorn using Composer.
composer require popphp/popcorn
Or, require it in your composer.json file
"require": {
"popphp/popcorn" : "^4.1.4"
}
Quickstart
In a simple index.php file, you can define the routes you want to allow
in your application. In this example, closures are used as the controllers.
The wildcard route * can serve as a "catch-all" to handle routes that are
not found or not allowed.
use Popcorn\Pop; $app = new Pop(); // Home page: GET http://localhost/ $app->get('/', function() { echo 'Hello World!'; }); // Say hello page: GET http://localhost/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.'; });
The above example defines two GET routes and wildcard to handle failures.
We can define a POST route like in this example below:
// Post auth route: POST http://localhost/auth $app->post('/auth', function() { if ($_SERVER['HTTP_AUTHORIZATION'] == 'my-token') { echo 'Auth successful'; } else { echo 'Auth failed'; } }); $app->run();
If you attempted access that above URL via GET (or any method that wasn't POST),
it would fail. If you access that URL via POST, but with the wrong token, it will
return the Auth failed message as enforced by the application. Access the URL
via POST with the correct token, and it will be successful.
$ curl -X POST --header "Authorization: bad-token" http://localhost/auth
Auth failed
$ curl -X POST --header "Authorization: my-token" http://localhost/auth
Auth successful
Advanced
In a more advanced example, we can take advantage of more of an MVC-style
of wiring up an application using the core components of Pop PHP with
Popcorn. Keeping it simple, let's look at a controller class
MyApp\Controller\IndexController like this:
<?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); } }
and two view scripts, index.phtml and error.phtml, respectively:
<!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>
Then we can set the app like this:
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();
The default parameter sets the controller as the default controller to handle
routes that aren't found. Typically, there is a default action in the controller,
such as an error method, to handle this.
Custom Methods
If your web server allows the configuration of custom HTTP methods, Popcorn supports that and allows you to register custom HTTP methods with the application.
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();
Then you can submit requests with your custom HTTP methods like this:
$ curl -X PURGE http://localhost/image/1
$ curl -X COPY http://localhost/image/1
popphp/popcorn 适用场景与选型建议
popphp/popcorn 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.69k 次下载、GitHub Stars 达 36, 最近一次更新时间为 2014 年 12 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「popcorn」 「php micro framework」 「rest framework」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 popphp/popcorn 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 popphp/popcorn 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 popphp/popcorn 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Anax htmlform module.
Micro Active Record library in PHP, support chain calls, events, and relations.
Onix Micro Framework
PHP classes for database handling upon PDO
Alfabank REST API integration
A weather module for the anax framework.
统计信息
- 总下载量: 7.69k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 37
- 点击次数: 4
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2014-12-06