PHP code example of amsrafid / piper-framework

1. Go to this page and download the library: Download amsrafid/piper-framework 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/ */

    

amsrafid / piper-framework example snippets


class ShopController extends Controller
{
    public function index() { }                 // GET /shop/index, nothing declared

    #[Get('/shop/{slug}')]                      // declared, because a slug is not a convention
    public function bySlug($slug) { }
}

use Piper\Routing\Attributes\Get;
use Piper\Routing\Attributes\Middleware;
use Piper\Routing\Attributes\Post;
use Piper\Routing\Attributes\Prefix;
use Piper\Routing\Attributes\Route;

#[Prefix('/api/v2')]
#[Middleware('auth:api')]
class OrderController extends Controller
{
    #[Get]
    public function index() { }                        // GET /api/v2/order/index

    #[Get('/orders/{slug}/lines/{line}')]
    public function lines($line, $slug) { }            // parameters arrive by name, in any order

    #[Get('/orders/{id:\d+}')]
    public function show($id) { }                      // constrained

    #[Get('/orders/page/{number?}')]
    public function page($number = 1) { }              // optional

    #[Post('/orders')]
    #[Middleware('throttle')]
    public function store(Request $request) { }        // type hints still come from the container

    #[Route('/health', methods: ['GET', 'HEAD'])]
    public function health() { }
}

public function __construct()
{
    $this->post('store');                    // restrict a method to a verb
    $this->middleware('auth');               // controller-wide
    $this->middleware(['auth:api' => 'store']);
}

protected $middleware = [
    \Piper\Foundation\Middleware\KeyValidation::class,
];

protected $prefixMiddleware = [
    '/api' => [\Piper\Authentication\Middleware\ApiDefault::class],
];

protected $routeMiddleware = [
    'auth:api' => \App\Http\Middleware\AuthApi::class,
];

return $this->response(['id' => 7], 201, ['Location' => '/api/orders/7']);
return $this->response($csv, 200, ['Content-Type' => 'text/csv; charset=utf-8']);