1. Go to this page and download the library: Download unquam/nette-maker 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/ */
unquam / nette-maker example snippets
declare(strict_types=1);
namespace App\Presentation\Article;
use Nette\Application\UI\Presenter;
final class ArticlePresenter extends Presenter
{
public function renderDefault(): void
{
}
}
declare(strict_types=1);
namespace App\Model;
use Nette\Database\Explorer;
final class Article
{
public function __construct(private Explorer $explorer)
{
}
}
// String format
'email' => '['
declare(strict_types=1);
namespace App\Presentation\Api\Requests\Article;
use Unquam\NetteMaker\Requests\FormRequest;
class StoreRequest extends FormRequest
{
public function rules(): array
{
return [
'title' => 'ovinný.',
'title.min_length' => 'Název musí mít alespoň :min znaků.',
];
}
}
public function messages(): array
{
return [
'email.resu.',
'password.min_length' => 'Heslo musí mít alespoň :min znaků.',
];
}
public function actionCreate(): void
{
try {
$request = new \App\Presentation\Api\Requests\Article\StoreRequest($this->getHttpRequest());
$validatedData = $request->validate(); // Safe, explicit verification loop $this->model->create($validatedData); $this->sendJson(['status' => 'success']);
} catch (\Unquam\NetteMaker\Exceptions\ValidationException $e) {
$this->getHttpResponse()->setCode($e->getCode());
$this->sendJson([
'message' => 'The given data was invalid.',
'errors' => $e->getErrors()
]);
}
}
public function actionSave(): void
{
try {
$request = new \App\Presentation\Requests\User\UpdateRequest($this->getHttpRequest());
$validatedData = $request->validate();
$this->model->save($validatedData);
$this->flashMessage('Profil byl úspěšně aktualizován!', 'success');
$this->redirect('User:default');
} catch (\Unquam\NetteMaker\Exceptions\ValidationException $e) {
// Handle failed states via standard Nette flash messages and redirect back
foreach ($e->getErrors() as $errorText) {
$this->flashMessage($errorText, 'danger');
}
$this->redirect('this');
}
}
public function rules(): array
{
return [
'email' => '
public function actionCreate(): void
{
try {
$request = new StoreUserRequest($this->getHttpRequest(), $this->explorer);
$validated = $request->validate();
$this->model->create($validated);
$this->sendJson(['status' => 'success']);
} catch (\Unquam\NetteMaker\Exceptions\ValidationException $e) {
$this->getHttpResponse()->setCode($e->getCode());
$this->sendJson(['errors' => $e->getErrors()]);
}
}
declare(strict_types=1);
namespace App\Presentation\Api\Resources;
use Unquam\NetteMaker\Resources\JsonResource;
class UserResource extends JsonResource
{
public function toArray(): array
{
return [
'id' => (int) $this->resource->id,
'email' => (string) $this->resource->email,
];
}
}
declare(strict_types=1);
namespace App\Presentation\Api\Resources;
use Unquam\NetteMaker\Resources\ResourceCollection;
class UserCollection extends ResourceCollection
{
protected function collectWith(): string
{
return UserResource::class;
}
}
// Select records for page 2, limiting to 15 entries per page
$users = $this->explorer->table('users')->page(2, 15);
// Automatically injects structured data and pagination meta hashes
$this->sendJson(UserCollection::make($users));
declare(strict_types=1);
return new class
{
public function run(\PDO $pdo): void
{
// Require the generated factory class layout
// Or create separate entries while seamlessly overriding default values
$factory->count(2)->create([
'role' => 'admin',
]);
}
};
declare(strict_types=1);
use Unquam\NetteMaker\Migration\TableBuilder;
return new class
{
public function up(TableBuilder $builder): void
{
$builder->create('{{table}}', function (TableBuilder $table): void {
$table->id();
// Available column types:
// $table->string('title');
// $table->string('slug', 191);
// $table->text('body');
// $table->integer('views');
// $table->bigInteger('score');
// $table->boolean('is_active');
// $table->float('rating');
// $table->decimal('price', 10, 2);
// $table->timestamp('published_at');
// $table->timestamps();
// Modifiers (chain after a column):
// $table->string('email')->nullable();
// $table->string('role')->default('user');
// $table->string('email')->unique();
// $table->string('name')->after('id'); // MySQL/MariaDB only
// Indexes:
// $table->index('user_id');
// $table->index(['user_id', 'status']);
// Foreign keys:
// $table->foreign('user_id', 'users')->cascadeOnDelete();
// $table->foreign('user_id', 'users', 'id', null, 'SET NULL', 'RESTRICT');
// Composite primary key:
// $table->primary(['user_id', 'role_id']);
$table->timestamps();
});
}
public function down(TableBuilder $builder): void
{
$builder->drop('{{table}}');
// To alter an existing table instead of dropping:
// $builder->table('{{table}}', function (TableBuilder $table): void {
// $table->dropColumn('email');
// $table->dropIndex('idx_{{table}}_email');
// $table->dropForeign('fk_{{table}}_user_id');
// $table->dropPrimary();
// });
}
};
$builder->create('table_name', function (TableBuilder $table): void {
$table->id(); // Auto-increment primary key
$table->string('title'); // VARCHAR(255) NOT NULL
$table->string('slug', 191); // VARCHAR(191) NOT NULL
$table->text('body'); // TEXT NOT NULL
$table->integer('views'); // INT NOT NULL
$table->bigInteger('score'); // BIGINT NOT NULL
$table->boolean('is_published'); // BOOLEAN/TINYINT NOT NULL
$table->float('rating'); // FLOAT NOT NULL
$table->decimal('price', 10, 2); // DECIMAL(10,2) NOT NULL
$table->timestamp('published_at'); // TIMESTAMP NULL
$table->timestamps(); // created_at + updated_at
// Modifiers (chain after a column):
$table->string('email')->nullable();
$table->string('role')->default('user');
$table->string('email')->unique();
$table->string('name')->after('id'); // MySQL/MariaDB only
// Indexes:
$table->index('user_id');
$table->index(['user_id', 'status']);
$table->index(['user_id', 'status'], 'custom_index_name');
// Foreign keys:
$table->foreign('user_id', 'users');
$table->foreign('user_id', 'users')->cascadeOnDelete();
$table->foreign('user_id', 'users', 'id', null, 'SET NULL', 'RESTRICT');
// Composite primary key:
$table->primary(['user_id', 'role_id']);
});
// Alter existing table:
$builder->table('table_name', function (TableBuilder $table): void {
$table->dropColumn('email');
$table->dropIndex('idx_table_email');
$table->dropForeign('fk_table_user_id');
$table->dropPrimary();
});
$builder->drop('table_name');
$builder->dropIfExists('table_name');