CLI Tools & Commands
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!
Running CLI Commands
How to Execute Commands
GEMVC CLI commands can be run in two ways:
Method 1: Using PHP Vendor Binary
php vendor/bin/gemvc <command>
Example: php vendor/bin/gemvc init
Method 2: Using Composer Scripts
composer gemvc <command>
Example: composer gemvc create:crud Product
Command Syntax:
All commands follow this pattern:
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)
Project Management Commands
Initialize New Project
The init command sets up a new GEMVC project with your chosen webserver configuration.
gemvc init [flags]
Interactive Mode:
Run without flags to get interactive prompts:
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:
# 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/) - ✓
.envfile fromexample.env - ✓
composer.jsonwith dependencies - ✓ Example User service (ready-to-use template)
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!
gemvc create:crud <ServiceName>
Example:
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
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
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
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
gemvc create:table <TableName>
# Example
gemvc create:table Product
Creates app/table/ProductTable.php with basic structure
Database Commands
Database Management
GEMVC provides powerful database management commands for initializing databases, migrating tables, and managing schema:
Initialize Database
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
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
gemvc db:list
Lists all tables in the database with their column information.
Describe Table
gemvc db:describe <TableName>
# Example
gemvc db:describe products
Shows detailed information about a table: columns, types, indexes, constraints.
Drop Table
gemvc db:drop <TableName>
# Example
gemvc db:drop products
⚠️ Warning: Permanently deletes the table and all its data!
Add Unique Constraint
gemvc db:unique <TableName> <ColumnName>
# Example
gemvc db:unique users email
Adds a unique constraint to a table column.
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:
gemvc init --swoole
Sets up project structure, copies templates, creates .env file
gemvc db:init
Creates the database from DB_NAME in .env
gemvc create:crud Product
Generates all 4 layers: API, Controller, Model, Table
gemvc db:migrate ProductTable
Creates products table in database with all columns and constraints
You now have a fully functional Product CRUD API with all 4 layers!
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 |
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.
# ✅ 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.
gemvc create:crud Product
gemvc db:migrate ProductTable
💡 Use Descriptive Names
Use PascalCase for service/model names. GEMVC will automatically handle naming conventions.
# ✅ 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/gemvcorcomposer gemvc. -
Naming Convention: Use PascalCase for service/model names (e.g.,
Product, notproduct). -
Table Migration: Always run
db:migrateafter 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-interactiveor-nflag 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!