Popcorn PHP REST-Based Micro Framework 4.1.1
Released October 16, 2023
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.1+.
popcorn
is a component of Pop PHP Framework.
Install popcorn
using Composer.
composer require popphp/popcorn
Or, require it in your composer.json file
"require": {
"popphp/popcorn" : "^4.1.1"
}
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
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.
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