PHP code example of dietervyncke / dry-external-api
1. Go to this page and download the library: Download dietervyncke/dry-external-api 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/ */
dietervyncke / dry-external-api example snippets
use Tnt\ExternalApi\Facade\Api;
/**
* Get request
* string $version, string $pattern, $controller, $method
*/
Api::get('1', 'users/(?<id>\d+)', Acme\Controller\UserController::class, 'get');
/**
* Post request
* string $version, string $pattern, $controller, $method
*/
Api::post('1', 'users', UserController::class, 'create');
/**
* Patch request
* string $version, string $pattern, $controller, $method
*/
Api::patch('1', 'users/(?<id>\d+)', UserController::class, 'update');
/**
* Put request
* string $version, string $pattern, $controller, $method
*/
Api::put('1', 'users/(?<id>\d+)', UserController::class, 'update');
/**
* Delete request
* string $version, string $pattern, $controller, $method
*/
Api::delete('1', 'users/(?<id>\d+)', UserController::class, 'delete');
namespace Acme\Controller;
use model\User;
use dry\db\FetchException;
use dry\db\DuplicateEntryException;
use Tnt\ExternalApi\Exception\ApiException;
use Tnt\ExternalApi\Http\Request;
class UserController
{
public static function get(Request $request)
{
try {
$user = User::load($request->parameters->integer('id'));
} catch (FetchException $e) {
throw new ApiException('user_not_found');
}
return [
'email' => $user->email,
'firstName' => $user->first_name,
'lastName' => $user->last_name,
];
}
public function create(Request $request)
{
$request->validate([
'email',
'password',
]);
try {
User::load_by( 'email', $request->data->string( 'email' ) );
} catch (DuplicateEntryException $e) {
throw new ApiException('duplicate_user');
} catch(FetchException $e) {}
$user = new User();
$user->email = $request->data->string( 'email' );
$user->password = $request->data->string( 'password' );
$user->save();
}
}