PHP code example of aliengen / pachyderm

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

    

aliengen / pachyderm example snippets


use Pachyderm\Dispatcher;
use Pachyderm\Exchange\Response;

$dispatcher = new Dispatcher();

// Declare a new GET endpoint
$dispatcher->get('/my_endpoint', function() {
    return Response::success(['success' => true]);
});

// Dispatch the request
$dispatcher->dispatch();

use Pachyderm\Dispatcher;
use Pachyderm\Middleware\MiddlewareManager;
use Pachyderm\Middleware\PreflightRequestMiddleware;
use Pachyderm\Middleware\TimerMiddleware;
use Pachyderm\Middleware\DbSessionMiddleware;
use Pachyderm\Middleware\SessionMiddleware;
use Pachyderm\Middleware\SessionAuthMiddleware;
use Pachyderm\Middleware\JSONEncoderMiddleware;
use Pachyderm\Exchange\Response;

/*
 * Instantiate the dispatcher with a base URL and middleware manager.
 */
$dispatcher = new Dispatcher('/api',  new MiddlewareManager());

/* Declaration of the middleware. */
$dispatcher->registerMiddlewares([
    JSONEncoderMiddleware::class,
    PreflightRequestMiddleware::class,
    SessionMiddleware::class,
    SessionAuthMiddleware::class,
    TimerMiddleware::class,
    DbSessionMiddleware::class
]);

/**
 * Declaration of the routes.
 */

$dispatcher->get('/my_endpoint', function() {
    return Response::success(['success' => true]);
});

$dispatcher->post('/my_post_endpoint', function($data) {
    return Response::success(['success' => true]);
});

/**
 * Dispatch the request.
 */
$dispatcher->dispatch();

$dispatcher->registerMiddlewares([
    JSONEncoderMiddleware::class,
    PreflightRequestMiddleware::class,
    // Add more middleware as needed
]);

$dispatcher->get('/my_endpoint', function() {
    return Response::success(['success' => true]);
}, 
[
    // Local middleware to be applied only to this route
    CustomMiddleware::class
], 
[
    // Global middleware to be excluded from this route
    JSONEncoderMiddleware::class
]);

  $dispatcher->get('/example', function() {
      return Response::success(['message' => 'Hello, World!']);
  });
  

  $dispatcher->post('/submit', function($data) {
      // Process $data
      return Response::success(['status' => 'Data submitted successfully']);
  });
  

  $dispatcher->put('/update/{id}', function($id, $data) {
      // Update data with $id
      return Response::success(['status' => 'Data updated successfully']);
  });
  

  $dispatcher->delete('/delete/{id}', function($id) {
      // Delete data with $id
      return Response::success(['status' => 'Data deleted successfully']);
  });
  

  $dispatcher->request('OPTIONS', '/options', function() {
      return Response::success(['methods' => 'GET, POST, PUT, DELETE']);
  });
  

  $dispatcher->request('HEAD', '/headers', function() {
      return Response::success(['methods' => 'GET, POST, PUT, DELETE']);
  });
  

use Pachyderm\Service;
use Pachyderm\Db;

Service::set('db', function() {
    return new Db([
        'host' => 'your_db_host',
        'username' => 'your_db_username',
        'password' => 'your_db_password',
        'database' => 'your_db_name'
    ]);
});

$db = Service::get('db');
$results = $db->findAll('users');

use Pachyderm\Db;

// Define your database configuration parameters
$parameters = [
    'host' => 'your_db_host',
    'username' => 'your_db_username',
    'password' => 'your_db_password',
    'database' => 'your_db_name'
];

// Create a new instance of the Db class
$db = new Db($parameters);

// Use the $db instance to perform database operations
$results = $db->findAll('users');

use Pachyderm\Dispatcher;
use Pachyderm\Middleware\MiddlewareManager;
use Pachyderm\Middleware\ExceptionHandlerMiddleware;

$dispatcher = new Dispatcher('/api', new MiddlewareManager());

// Register the ExceptionHandlerMiddleware
$dispatcher->registerMiddlewares([
    ExceptionHandlerMiddleware::class,
    // Other middlewares...
]);

// Define a route that throws a BadRequestException
$dispatcher->get('/example', function() {
    // Simulate a condition that causes a bad request
    $condition = false;
    if (!$condition) {
        throw new BadRequestException('Invalid request parameters.');
    }
    return Response::success(['success' => true]);
});

// Dispatch the request
$dispatcher->dispatch();