PHPackages                             popphp/popcorn - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. popphp/popcorn

ActiveLibrary[Framework](/categories/framework)

popphp/popcorn
==============

Popcorn, A REST-Based PHP Micro Framework

4.1.4(6mo ago)376.9k↓50%32BSD-3-ClausePHPPHP &gt;=8.3.0CI passing

Since Aug 11Pushed 6mo ago4 watchersCompare

[ Source](https://github.com/popphp/popcorn)[ Packagist](https://packagist.org/packages/popphp/popcorn)[ Docs](https://github.com/popphp/popcorn)[ RSS](/packages/popphp-popcorn/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (36)Used By (2)

Popcorn PHP Micro Framework
===========================

[](#popcorn-php-micro-framework)

[![](https://camo.githubusercontent.com/486866b6193cf7e2d3b13a6bf6893a4374ae9a7db8c8d66015e37d7cbb6cd33c/687474703a2f2f7777772e706f707068702e6f72672f6173736574732f696d672f706f70636f726e2d6c6f676f2d736861646f772e706e67)](https://camo.githubusercontent.com/486866b6193cf7e2d3b13a6bf6893a4374ae9a7db8c8d66015e37d7cbb6cd33c/687474703a2f2f7777772e706f707068702e6f72672f6173736574732f696d672f706f70636f726e2d6c6f676f2d736861646f772e706e67)

[![Build Status](https://github.com/popphp/popcorn/workflows/phpunit/badge.svg)](https://github.com/popphp/popcorn/actions)[![Coverage Status](https://camo.githubusercontent.com/08638e28f92cdb9863f5545508c8588bde346296e9ac25304f3e20dcd2132249/687474703a2f2f63632e706f707068702e6f72672f636f7665726167652e7068703f636f6d703d706f70636f726e)](http://cc.popphp.org/popcorn/)

[![Join the chat at https://discord.gg/TZjgT74U7E](https://camo.githubusercontent.com/acad7b0eeb78b78d08ffd2b85681ab243436388b5f86f8bcb956a69246e53739/68747470733a2f2f6d656469612e706f707068702e6f72672f696d672f646973636f72642e737667)](https://discord.gg/TZjgT74U7E)

- [Overview](#overview)
- [Install](#install)
- [Quickstart](#quickstart)
- [Advanced](#advanced)
- [Custom Methods](#custom-methods)

RELEASE INFORMATION
-------------------

[](#release-information)

Popcorn PHP REST-Based Micro Framework 4.1.2
Released December 2, 2024

Overview
--------

[](#overview)

Popcorn PHP Micro Framework is a REST-based micro framework. It is a small component that acts as a layer for [Pop PHP](https://github.com/popphp/popphp) to enforce the REST-based routing rules of a web application. It supports PHP 8.2+.

`popcorn` is a component of [Pop PHP Framework](http://www.popphp.org/).

[Top](#popcorn-php-micro-framework)

Install
-------

[](#install)

Install `popcorn` using Composer.

```
composer require popphp/popcorn

```

Or, require it in your composer.json file

```
"require": {
    "popphp/popcorn" : "^4.1.4"
}

```

[Top](#popcorn-php-micro-framework)

Quickstart
----------

[](#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
```

[Top](#popcorn-php-micro-framework)

Advanced
--------

[](#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:

```
