2017 © Pedro Peláez
 

library popcorn

Popcorn, A REST-Based PHP Micro Framework

image

popphp/popcorn

Popcorn, A REST-Based PHP Micro Framework

  • Monday, January 29, 2018
  • by nicksagona
  • Repository
  • 3 Watchers
  • 19 Stars
  • 684 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 19 Versions
  • 8 % Grown

The README.md

Popcorn PHP Micro Framework

, (*1)

Build Status Coverage Status, (*2)

Join the chat at https://discord.gg/TZjgT74U7E, (*3)

RELEASE INFORMATION

Popcorn PHP REST-Based Micro Framework 4.1.2
Released December 2, 2024, (*4)

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+., (*5)

popcorn is a component of Pop PHP Framework., (*6)

Top, (*7)

Install

Install popcorn using Composer., (*8)

composer require popphp/popcorn

Or, require it in your composer.json file, (*9)

"require": {
    "popphp/popcorn" : "^4.1.3"
}

Top, (*10)

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., (*11)

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., (*12)

We can define a POST route like in this example below:, (*13)

// 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., (*14)

$ 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, (*15)

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:, (*16)

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:

```php





    =$title; ?></title>
</head>

<body>
    <h1><?=$title; ?></h1>
</body>

</html>
<!DOCTYPE html>

<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:, (*17)

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., (*18)

Top, (*19)

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., (*20)

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:, (*21)

$ curl -X PURGE http://localhost/image/1
$ curl -X COPY http://localhost/image/1

Top, (*22)

The Versions

29/01 2018

v2.x-dev

2.9999999.9999999.9999999-dev http://popcorn.popphp.org/

Popcorn, A REST-Based PHP Micro Framework

  Sources   Download

BSD-3-Clause New BSD

The Requires

 

The Development Requires

popcorn php micro framework rest framework

29/01 2018

dev-master

9999999-dev http://popcorn.popphp.org/

Popcorn, A REST-Based PHP Micro Framework

  Sources   Download

BSD-3-Clause New BSD

The Requires

 

The Development Requires

popcorn php micro framework rest framework

29/01 2018

dev-v3-dev

dev-v3-dev http://popcorn.popphp.org/

Popcorn, A REST-Based PHP Micro Framework

  Sources   Download

BSD-3-Clause New BSD

The Requires

 

The Development Requires

popcorn php micro framework rest framework

29/01 2018

3.1.6

3.1.6.0 http://popcorn.popphp.org/

Popcorn, A REST-Based PHP Micro Framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

popcorn php micro framework rest framework

27/09 2017

3.1.5

3.1.5.0 http://popcorn.popphp.org/

Popcorn, A REST-Based PHP Micro Framework

  Sources   Download

New BSD

The Requires

 

The Development Requires

popcorn php micro framework rest framework

14/09 2017

3.1.4

3.1.4.0 http://popcorn.popphp.org/

Popcorn, A REST-Based PHP Micro Framework

  Sources   Download

New BSD

The Requires

 

The Development Requires

popcorn php micro framework rest framework

13/09 2017

3.1.3

3.1.3.0 http://popcorn.popphp.org/

Popcorn, A REST-Based PHP Micro Framework

  Sources   Download

New BSD

The Requires

 

The Development Requires

popcorn php micro framework rest framework

21/06 2017

3.1.2

3.1.2.0 http://popcorn.popphp.org/

Popcorn, A REST-Based PHP Micro Framework

  Sources   Download

New BSD

The Requires

 

The Development Requires

popcorn php micro framework rest framework

25/04 2017

3.1.1

3.1.1.0 http://popcorn.popphp.org/

Popcorn, A REST-Based PHP Micro Framework

  Sources   Download

New BSD

The Requires

 

The Development Requires

popcorn php micro framework rest framework

22/02 2017

3.1.0

3.1.0.0 http://popcorn.popphp.org/

Popcorn, A REST-Based PHP Micro Framework

  Sources   Download

New BSD

The Requires

 

The Development Requires

popcorn php micro framework rest framework

03/10 2016

3.0.1

3.0.1.0 http://popcorn.popphp.org/

Popcorn, A REST-Based PHP Micro Framework

  Sources   Download

New BSD

The Requires

 

The Development Requires

popcorn php micro framework rest framework

10/07 2016

3.0.0

3.0.0.0 http://popcorn.popphp.org/

Popcorn, A REST-Based PHP Micro Framework

  Sources   Download

New BSD

The Requires

 

The Development Requires

popcorn php micro framework rest framework

09/07 2016

2.1.1

2.1.1.0 http://popcorn.popphp.org/

Popcorn, A REST-Based PHP Micro Framework

  Sources   Download

New BSD

The Requires

 

The Development Requires

popcorn php micro framework rest framework

01/07 2016

2.1.0

2.1.0.0 http://popcorn.popphp.org/

Popcorn, A REST-Based PHP Micro Framework

  Sources   Download

New BSD

The Requires

 

The Development Requires

popcorn php micro framework rest framework

10/05 2016

2.0.2p1

2.0.2.0-patch1 http://popcorn.popphp.org/

Popcorn, A REST-Based PHP Micro Framework

  Sources   Download

New BSD

The Requires

 

The Development Requires

popcorn php micro framework rest framework

29/02 2016

2.0.2

2.0.2.0 http://popcorn.popphp.org/

Popcorn, A REST-Based PHP Micro Framework

  Sources   Download

New BSD

The Requires

 

The Development Requires

popcorn php micro framework rest framework

19/02 2016

2.0.1p1

2.0.1.0-patch1 http://popcorn.popphp.org/

Popcorn, A REST-Based PHP Micro Framework

  Sources   Download

New BSD

The Requires

 

The Development Requires

popcorn php micro framework rest framework

27/10 2015

2.0.1

2.0.1.0 http://popcorn.popphp.org/

Popcorn, A REST-Based PHP Micro Framework

  Sources   Download

New BSD

The Requires

 

The Development Requires

popcorn php micro framework rest framework

11/08 2015

2.0.0

2.0.0.0 http://popcorn.popphp.org/

Popcorn, A REST-Based PHP Micro Framework

  Sources   Download

New BSD

The Requires

 

The Development Requires

popcorn php micro framework rest framework