PHP code example of popphp / popcorn

1. Go to this page and download the library: Download popphp/popcorn library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

popphp / popcorn example snippets


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.';
});

// 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();



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);
    }

}

<!DOCTYPE html>
<!-- index.phtml //-->
<html>

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

<!DOCTYPE html>
<!-- error.phtml //-->
<html>

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

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();

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();