CLI Commands
ApnaPHP provides a powerful CLI (Command Line Interface) tool called apna that helps you generate files, manage your application, and speed up development.
1. Basic Commands
To see a list of all available commands, run:
php apna list
# or simply
php apna
Some common basic commands include:
php apna help [command]: Displays help for a specific command.php apna version: Shows the current ApnaPHP framework version.php apna serve: Starts a local development server.php apna routes: Lists all registered routes in your application.
Starting the Development Server
php apna serve
This will start a server, typically at http://localhost:3000.
Options:
--host=<host_address>: Specify the host address (default: localhost)--port=<port_number>: Specify the port number (default: 3000, auto-increments if in use)--public=<directory>: Specify the public directory to serve (default: public)
Examples:
php apna serve --host=0.0.0.0 --port=8080
php apna serve --port=5000 --public=public_html
Listing Routes
php apna routes
This command displays all registered routes in your application, organized by type (regular routes vs API routes).
Example Output:
π ApnaPHP Framework - Available Routes
π Regular Routes:
βββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββ
β METHOD β URI β HANDLER β
βββββββββββΌββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββ€
β GET β / β app/page.apna.php β
β GET β /about β app/about/page.apna.php β
β GET β /blog/{slug} β app/blog/[slug]/page.apna.php β
βββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββ
π API Routes:
βββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββ
β METHOD β URI β HANDLER β
βββββββββββΌββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββ€
β GET β /api/users β app/api/users/route.apna.php@GET β
β POST β /api/users β app/api/users/route.apna.php@POST β
β GET β /api/posts/{id} β app/api/posts/[id]/route.apna.php@GET β
βββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββ
2. Generator Commands (make:*)
These commands help you quickly scaffold common application components.
make:model
Creates a new model class with auto-migration support.
php apna make:model <name>
Options:
--table=<table_name>: Specify the table name for the model--migration: Create a migration file for the model--schema=<schema_definition>: Define the schema for the model
Examples:
# Basic model
php apna make:model User
# Model with custom table name
php apna make:model Post --table=blog_posts
# Model with migration
php apna make:model Product --migration
# Model with schema definition
php apna make:model Category --schema="name:required|type:string|length:255,slug:required|type:string|length:255|unique"
Generated Model Example:
<?php
namespace App\Models;
use ApnaPHP\Database\Model;
class User extends Model
{
protected string $table = 'users';
protected string $primaryKey = 'id';
protected bool $autoMigrate = true;
protected array $schema = [
'name' => 'required|type:string|length:255',
'email' => 'required|unique|type:string|length:255',
'password' => 'required|type:string',
];
protected array $fillable = [
'name', 'email', 'password'
];
protected array $hidden = ['password'];
}
make:page
Creates a new page file.
php apna make:page <name>
Examples:
# Basic page
php apna make:page About
# Nested page
php apna make:page Blog/Post
# Dynamic page
php apna make:page Blog/[slug]
Generated Page Example:
<?php
// app/about/page.apna.php
metadata([
'title' => 'About',
'description' => 'About page description'
]);
?>
<h1>About</h1>
<p>Welcome to the about page!</p>
make:route
Creates a new API route file.
php apna make:route <name>
Examples:
# Basic API route
php apna make:route api/users
# Nested API route
php apna make:route api/posts/comments
# Dynamic API route
php apna make:route api/users/[id]
Generated Route Example:
<?php
// app/api/users/route.apna.php
use ApnaPHP\Routing\Request;
use ApnaPHP\Routing\Response;
function GET(Request $request): Response
{
// Handle GET request
return Response::json(['message' => 'GET /api/users']);
}
function POST(Request $request): Response
{
// Handle POST request
return Response::json(['message' => 'POST /api/users']);
}
function PUT(Request $request): Response
{
// Handle PUT request
return Response::json(['message' => 'PUT /api/users']);
}
function DELETE(Request $request): Response
{
// Handle DELETE request
return Response::json(['message' => 'DELETE /api/users']);
}
make:middleware
Creates a new middleware file.
php apna make:middleware <name>
Examples:
# Basic middleware
php apna make:middleware AuthCheck
# Nested middleware
php apna make:middleware Admin/AccessControl
Generated Middleware Example:
<?php
// app/middleware/AuthCheck.php
use ApnaPHP\Routing\Request;
use ApnaPHP\Routing\Response;
// Check if user is authenticated
if (!auth()->check()) {
return redirect('/login')->withError('Please login to continue.');
}
// If authenticated, continue to the next middleware or route handler
make:migration
Creates a new database migration file.
php apna make:migration <name>
Examples:
# Basic migration
php apna make:migration create_users_table
# Complex migration
php apna make:migration add_columns_to_posts_table
Generated Migration Example:
<?php
// migrations/2024_01_15_120000_create_users_table.php
use ApnaPHP\Database\SchemaBuilder;
class CreateUsersTable
{
public function up(SchemaBuilder $schema)
{
$schema->create('users', function ($table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->timestamps();
});
}
public function down(SchemaBuilder $schema)
{
$schema->drop('users');
}
}
3. Command Options
Many commands accept options to modify their behavior.
serve Command Options
--host=<host_address>: Specify the host for the development server.php apna serve --host=0.0.0.0--port=<port_number>: Specify the port for the development server.php apna serve --port=8080--public=<directory>: Specify the public directory to serve.php apna serve --public=public_html
make:model Command Options
--table=<table_name>: Specify the table name for the model.php apna make:model User --table=users_table--migration: Create a migration file for the model.php apna make:model Post --migration--schema=<schema_definition>: Define the schema for the model.php apna make:model Category --schema="name:required|type:string|length:255,slug:required|type:string|length:255|unique"
4. Command Examples
Complete Development Workflow
# 1. Start development server
php apna serve
# 2. Create a new model with migration
php apna make:model Post --migration --schema="title:required|type:string|length:255,content:required|type:text,author_id:required|type:integer"
# 3. Create API routes for the model
php apna make:route api/posts
php apna make:route api/posts/[id]
# 4. Create pages
php apna make:page Blog
php apna make:page Blog/[slug]
# 5. Create middleware for authentication
php apna make:middleware AuthCheck
# 6. List all routes
php apna routes
# 7. Check version
php apna version
Model with Complete Schema
php apna make:model Product --schema="name:required|type:string|length:255,slug:required|type:string|length:255|unique,description:nullable|type:text,price:required|type:decimal|precision:10|scale:2|min:0,stock:type:integer|default:0|min:0,category_id:required|type:integer,status:type:string|default:active|in:active,inactive,featured:type:boolean|default:false"
This creates a Product model with:
name: Required string, max 255 charactersslug: Required unique string, max 255 charactersdescription: Optional text fieldprice: Required decimal with 10 precision, 2 scale, minimum 0stock: Integer with default 0, minimum 0category_id: Required integerstatus: String with default 'active', must be 'active', 'inactive', or 'featured'featured: Boolean with default false
5. Command Output Examples
serve Command Output
π ApnaPHP Development Server
β
Server started successfully!
π URL: http://localhost:3000
π Public Directory: public
β‘ PHP Version: 8.1.0
π§ Framework: ApnaPHP v1.0.0
Press Ctrl+C to stop the server
routes Command Output
π ApnaPHP Framework - Available Routes
π Regular Routes:
βββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββ
β METHOD β URI β HANDLER β
βββββββββββΌββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββ€
β GET β / β app/page.apna.php β
β GET β /about β app/about/page.apna.php β
β GET β /blog β app/blog/page.apna.php β
β GET β /blog/{slug} β app/blog/[slug]/page.apna.php β
βββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββ
π API Routes:
βββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββ
β METHOD β URI β HANDLER β
βββββββββββΌββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββ€
β GET β /api/users β app/api/users/route.apna.php@GET β
β POST β /api/users β app/api/users/route.apna.php@POST β
β GET β /api/users/{id} β app/api/users/[id]/route.apna.php@GET β
β PUT β /api/users/{id} β app/api/users/[id]/route.apna.php@PUT β
β DELETE β /api/users/{id} β app/api/users/[id]/route.apna.php@DELETEβ
βββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββ
π Summary: 9 routes found (4 regular, 5 API)
make:model Command Output
β
Model created successfully!
π File: models/User.php
π§ Table: users
π Schema: name, email, password
π Auto-migration: enabled
π‘ Next steps:
1. Define your schema in the $schema property
2. Add fillable fields to $fillable array
3. Create your first user: User::create(['name' => 'John', 'email' => 'john@example.com'])
6. Best Practices
- Use Descriptive Names: Choose clear, descriptive names for your models, pages, and routes.
- Schema Definition: Use the
--schemaoption when creating models to define your database structure upfront. - Migration Creation: Always use
--migrationwhen creating models to ensure your database schema is properly managed. - Route Organization: Organize your routes logically using nested directories.
- Middleware Usage: Create middleware for common functionality like authentication, authorization, and logging.
- Development Server: Use the development server for local development and testing.
7. Troubleshooting
Common Issues
Port Already in Use:
php apna serve --port=3001
Permission Denied:
chmod +x apna
php apna serve
Model Not Found:
# Make sure you're in the project root directory
cd /path/to/your/project
php apna make:model User
Routes Not Showing:
# Make sure your app directory exists and contains .apna.php files
php apna routes
This comprehensive CLI system makes ApnaPHP development fast and efficient by automating common tasks and providing clear feedback about your application structure.
