1. Go to this page and download the library: Download oscarotero/folk 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/ */
oscarotero / folk example snippets
namespace MyEntities;
use Folk\SearchQuery;
use Folk\Formats\FormatFactory;
use Folk\Formats\Group;
use Folk\Entities\AbstractEntity;
/**
* Entity to manage the posts
*/
class Posts extends AbstractEntity
{
public $title = 'Posts';
public $description = 'These are the posts of the blog';
/**
* List the posts
*
* @return array [id => data, ...]
*/
public function search(SearchQuery $search): array
{
$query = 'SELECT * FROM posts';
if ($search->getPage() !== null) {
$limit = $search->getLimit();
$offset = ($search->getPage() * $limit) - $limit;
$query .= " LIMIT {$offset}, {$limit}";
}
$pdo = $this->admin->get('pdo');
$result = $pdo->query($query);
$data = [];
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$data[$row['id']] = $row;
}
return $data;
}
/**
* Create a new post
*
* @return mixed The post id
*/
public function create(array $data)
{
$pdo = $this->admin->get('pdo');
$statement = $pdo->prepare('INSERT INTO posts (title, text) VALUES (:title, :text)');
$statement->execute([
':title' => $data['title'],
':text' => $data['text'],
]);
return $pdo->lastInsertId();
}
/**
* Read a post
*
* @return array
*/
public function read($id): array
{
$pdo = $this->admin->get('pdo');
$statement = $pdo->prepare('SELECT * FROM posts WHERE id = ? LIMIT 1');
$statement->execute([$id]);
return $statement->fetch(PDO::FETCH_ASSOC);
}
/**
* Update a post
*/
public function update($id, array $data)
{
$pdo = $this->admin->get('pdo');
$statement = $pdo->prepare('UPDATE posts SET title = :title, text = :text WHERE id = :id LIMIT 1');
$statement->execute([
':title' => $data['title'],
':text' => $data['text'],
':id' => $data['id'],
]);
}
/**
* Delete a post
*/
public function delete($id)
{
$pdo = $this->admin->get('pdo');
$statement = $pdo->prepare('DELETE FROM posts WHERE id = ? LIMIT 1');
$statement->execute([$id]);
}
/**
* Returns the data scheme used by the posts.
*/
public function getScheme(FormatFactory $factory): Group
{
return $factory->group([
'title' => $factory->text()
->maxlength(200)
->label('The post title'),
'text' => $factory->html()
->label('The body'),
]);
}
/**
* Returns the label of a row.
* (used in autocomplete searches, select, etc)
*/
public function getLabel($id, array $data): string
{
return sprintf('%s (%d)', $data['title'], $id);
}
}
use Folk\Admin;
use Entities\Posts;
//Create a Admin instance passing the root path and the http uri:
$uri = new Zend\Diactoros\Uri('http://my-site.com/admin');
$admin = new Admin(__DIR__, $uri);
//Set the pdo instance:
$admin['pdo'] = new PDO('mysql:dbname=database;charset=UTF8');
//Add set your entities classes
$admin->setEntities([
Posts::class
]);
//Run the web (using PSR-7 request/responses)
$request = Zend\Diactoros\ServerRequestFactory::fromGlobals();
$emitter = new Zend\Diactoros\Response\SapiEmitter();
$response = $admin($request);
$emitter->emit($response);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.