CLI Tools & Commands

20 minutes INTERMEDIATE

Master GEMVC's easy yet powerful CLI commands. Learn project initialization, code generation, and database management with simple command-line tools.

What You'll Learn

Project Management

Initialize projects with gemvc init

Code Generation

Generate Services, Controllers, Models, and Tables

Database Commands

Manage databases with migration and utility commands

Video Coming Soon...

What is GEMVC CLI?

GEMVC comes with an easy yet powerful command-line interface that helps you build applications faster. The CLI provides commands for project initialization, code generation, and database management - all with simple, intuitive commands.

  • Project Management - Initialize projects with different webservers (OpenSwoole, Apache, Nginx)
  • Code Generation - Generate complete CRUD operations or individual layers (Service, Controller, Model, Table)
  • Database Management - Initialize databases, migrate tables, list tables, and manage schema
  • Template System - Customizable code generation templates
  • Non-Interactive Mode - Perfect for CI/CD pipelines

Key Point: GEMVC CLI commands are designed to be simple yet powerful. A single command like gemvc create:crud Product generates all 4 layers of your CRUD API!

1

Running CLI Commands

How to Execute Commands

GEMVC CLI commands can be run in two ways:

Method 1: Using PHP Vendor Binary

Terminal
php vendor/bin/gemvc <command>

Example: php vendor/bin/gemvc init

Method 2: Using Composer Scripts

Terminal
composer gemvc <command>

Example: composer gemvc create:crud Product

Command Syntax:

All commands follow this pattern:

Terminal
gemvc <command> [arguments] [flags]

<command> = Command name (e.g., init, create:crud)
[arguments] = Optional arguments (e.g., Product)
[flags] = Optional flags (e.g., --swoole, -cmt)

2

Project Management Commands

Initialize New Project

The init command sets up a new GEMVC project with your chosen webserver configuration.

Terminal
gemvc init [flags]

Interactive Mode:

Run without flags to get interactive prompts:

Terminal
gemvc init

The command will prompt you to:

  • ✓ Select webserver (OpenSwoole, Apache, Nginx)
  • ✓ Choose Docker setup (optional)
  • ✓ Install PHPStan (optional)
  • ✓ Install testing framework (optional)

Non-Interactive Mode:

Use flags to skip prompts:

Terminal
# OpenSwoole
gemvc init --swoole

# Apache
gemvc init --apache

# Nginx
gemvc init --nginx

# Or use --server flag
gemvc init --server=swoole
gemvc init --server=apache

# Non-interactive (skip all prompts)
gemvc init --swoole --non-interactive

What Gets Created:

  • ✓ Project directory structure (app/api, app/controller, app/model, app/table)
  • ✓ Webserver-specific files (index.php, Dockerfile)
  • ✓ Templates for code generation (templates/cli/)
  • .env file from example.env
  • composer.json with dependencies
  • ✓ Example User service (ready-to-use template)
3

Code Generation Commands

Generate Complete CRUD

The easiest way to create a complete CRUD API is using the create:crud command. It generates all 4 layers at once!

Terminal
gemvc create:crud <ServiceName>

Example:

Terminal
gemvc create:crud Product

Generated Files:

  • app/api/Product.php - API Service layer
  • app/controller/ProductController.php - Controller layer
  • app/model/ProductModel.php - Model layer
  • app/table/ProductTable.php - Table layer

What Gets Generated:

  • ✓ Full CRUD methods: create(), read(), update(), delete(), list()
  • ✓ Schema validation in API layer
  • ✓ Business logic in Controller layer
  • ✓ Data logic in Model layer
  • ✓ Database operations in Table layer
  • ✓ Helper methods (selectById(), selectByName())

Generate Individual Layers

You can also generate individual layers or combine them with flags:

Create API Service

Terminal
gemvc create:service <ServiceName> [flags]

# Create service only
gemvc create:service Product

# Create service + controller + model + table
gemvc create:service Product -cmt

Flags: -c (Controller), -m (Model), -t (Table), -cmt (all)

Create Controller

Terminal
gemvc create:controller <ControllerName> [flags]

# Create controller only
gemvc create:controller Product

# Create controller + model + table
gemvc create:controller Product -mt

Flags: -m (Model), -t (Table), -mt (both)

Create Model

Terminal
gemvc create:model <ModelName> [flags]

# Create model only
gemvc create:model Product

# Create model + table
gemvc create:model Product -t

Flags: -t (Table)

Create Table

Terminal
gemvc create:table <TableName>

# Example
gemvc create:table Product

Creates app/table/ProductTable.php with basic structure

4

Database Commands

Database Management

GEMVC provides powerful database management commands for initializing databases, migrating tables, and managing schema:

Initialize Database

Terminal
gemvc db:init

Creates the database specified in .env file (DB_NAME). Uses root credentials from DB_ROOT_USER and DB_ROOT_PASSWORD.

Migrate Table

Terminal
gemvc db:migrate <TableClassName> [flags]

# Basic migration
gemvc db:migrate ProductTable

# Force sync (remove missing columns)
gemvc db:migrate ProductTable --force

# Sync schema constraints
gemvc db:migrate ProductTable --sync-schema

# Set default value for new columns
gemvc db:migrate ProductTable --default="Active"

Creates or updates the database table based on your Table class definition. Reads $_type_map and defineSchema() to create columns, indexes, and constraints.

List Tables

Terminal
gemvc db:list

Lists all tables in the database with their column information.

Describe Table

Terminal
gemvc db:describe <TableName>

# Example
gemvc db:describe products

Shows detailed information about a table: columns, types, indexes, constraints.

Drop Table

Terminal
gemvc db:drop <TableName>

# Example
gemvc db:drop products

⚠️ Warning: Permanently deletes the table and all its data!

Add Unique Constraint

Terminal
gemvc db:unique <TableName> <ColumnName>

# Example
gemvc db:unique users email

Adds a unique constraint to a table column.

5

Complete Workflow Example

Building a Product API from Scratch

Here's a complete workflow showing how to use CLI commands to build a Product API:

1
Initialize Project
Terminal
gemvc init --swoole

Sets up project structure, copies templates, creates .env file

2
Initialize Database
Terminal
gemvc db:init

Creates the database from DB_NAME in .env

3
Create CRUD
Terminal
gemvc create:crud Product

Generates all 4 layers: API, Controller, Model, Table

4
Migrate Table
Terminal
gemvc db:migrate ProductTable

Creates products table in database with all columns and constraints

Done! Your API is Ready

You now have a fully functional Product CRUD API with all 4 layers!

6

Quick Command Reference

All Available Commands

Command Description Example
init Initialize new project gemvc init --swoole
create:crud Create complete CRUD gemvc create:crud Product
create:service Create API service gemvc create:service Product -cmt
create:controller Create controller gemvc create:controller Product -mt
create:model Create model gemvc create:model Product -t
create:table Create table class gemvc create:table Product
db:init Initialize database gemvc db:init
db:migrate Migrate table gemvc db:migrate ProductTable
db:list List all tables gemvc db:list
db:describe Describe table structure gemvc db:describe products
db:drop Drop table gemvc db:drop products
db:unique Add unique constraint gemvc db:unique users email
7

Flags and Options

Common Flags

GEMVC commands support various flags to customize behavior:

Code Generation Flags

-c - Create Controller
-m - Create Model
-t - Create Table
-cmt - Create all components

Used with: create:service, create:controller, create:model

Migration Flags

--force - Remove columns not in class definition
--sync-schema - Sync schema constraints (indexes, unique, etc.)
--enforce-not-null - Enforce NOT NULL constraints
--default=<value> - Set default value for new columns

Used with: db:migrate

Project Initialization Flags

--swoole - Initialize for OpenSwoole
--apache - Initialize for Apache
--nginx - Initialize for Nginx
--server=<name> - Specify webserver (swoole, apache, nginx)
--non-interactive or -n - Skip prompts, use defaults

Used with: init

Tips & Best Practices

💡 Use create:crud for Complete Setup

Instead of creating components separately, use create:crud to generate all 4 layers at once. It's faster and ensures consistency.

Terminal
# ✅ Recommended
gemvc create:crud Product

# ❌ Not recommended
gemvc create:service Product -cmt

💡 Always Migrate After Creating Tables

After creating a Table class, always run db:migrate to create the actual database table.

Terminal
gemvc create:crud Product
gemvc db:migrate ProductTable

💡 Use Descriptive Names

Use PascalCase for service/model names. GEMVC will automatically handle naming conventions.

Terminal
# ✅ Good
gemvc create:crud Product
gemvc create:crud UserProfile

# ❌ Avoid
gemvc create:crud product
gemvc create:crud user_profile

💡 Customize Templates

You can customize code generation templates in templates/cli/ directory. Edit templates to match your coding style, then regenerate code.

Important Notes

  • Command Location: Commands can be run using php vendor/bin/gemvc or composer gemvc.
  • Naming Convention: Use PascalCase for service/model names (e.g., Product, not product).
  • Table Migration: Always run db:migrate after creating or modifying Table classes to sync database schema.
  • Generated Code: CLI generates production-ready code following GEMVC's 4-layer architecture and best practices.
  • Non-Interactive Mode: Use --non-interactive or -n flag for CI/CD pipelines and automated scripts.

🛠️ CLI Tools Mastered!

Excellent! You've learned how to use GEMVC's powerful CLI commands. You can now initialize projects, generate code, and manage databases with simple commands!