PHP code example of unquam / nette-maker

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;
    }
}

$user = $this->explorer->table('users')->get(1);
$this->sendJson(UserResource::make($user));

// 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);

use Unquam\NetteMaker\Migration\AbstractFactory;

class UserFactory extends AbstractFactory
{
    protected function defineTable(): string
    {
        return 'users';
    }

    protected function definition(): array
    {
        // $faker = \Faker\Factory::create();

        return [
            'name' => 'John Doe',
            'email' => 'user_' . uniqid() . '@example.com',
            'role' => 'user',
            'created_at' => date('Y-m-d H:i:s'),
        ];
    }
}



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');

/** @var \Nette\DI\Container $container */
$app = $container->getByType(\Unquam\NetteMaker\Application::class);
exit($app->run());
bash
cp vendor/bin/nette-maker nette
chmod +x nette
php nette make:init
bash
php nette <command> [arguments] [options]
bash
php nette make:init
bash
php nette make:presenter Article
# → app/Presentation/Article/ArticlePresenter.php
bash
php nette make:model Article
# → app/Model/Article.php
bash
php nette make:repository Article
# → app/Model/Repositories/ArticleRepository.php
bash
php nette make:service Article
# → app/Model/Services/ArticleService.php
bash
php nette make:request Article/Store
bash
php nette make:request User/Update --web
bash
# Generate Nette Tester class (creates tests/Unit/Services/UserServiceTest.phpt)
php nette make:test Services/UserService

# Generate PHPUnit class (creates tests/Unit/Services/UserServiceTest.php)
php nette make:test Services/UserService --phpunit
bash
php nette make:test
bash
php nette make:test Model/Security/Authenticator
bash
php nette make:module Article
bash
php nette make:module Article --no-migration --no-latte
php nette make:module Article --no-service --no-repository
php nette make:module Article --no-presenter --no-model
bash
php nette make:module Article --only=presenter,model
php nette make:module Article --only=migration
php nette make:module Article --only=presenter,model,repository,service
bash
php nette make:auth
bash
php nette migrate
bash
php nette make:resource User
bash
php nette make:resource UserCollection
bash
php nette make:seeder UserSeeder
bash
php nette make:factory User
bash
php nette make:migration CreateArticlesTable
# → db/migrations/2026_05_18_120000_create_articles_table.php
bash
php nette migrate
bash
php nette migrate --status
bash
php nette migrate --rollback
bash
php nette migrate:fresh
bash
php nette db:seed
bash
php nette db:wipe
bash
php nette clear:cache