Skip to main content
The make:controller command generates a new controller class in the App/Controllers directory.

Syntax

php rawr make:controller <Name>
Name
string
required
The name of the controller class (use PascalCase)

Basic Usage

Create a new controller:
php rawr make:controller UserController
Output:
Controller created: App/Controllers/UserController.php

Generated File

The command creates a controller with a default index() method:
App/Controllers/UserController.php
<?php

declare(strict_types=1);

namespace App\Controllers;

use Lyger\Http\Request;
use Lyger\Http\Response;

class UserController
{
    public function index(): Response
    {
        return Response::json(['message' => 'UserController controller']);
    }
}
All generated controllers use strict types and return typed Response objects for better code quality.

Controller Structure

Namespace

Controllers are automatically namespaced under App\Controllers:
namespace App\Controllers;

Imports

Default imports include:
use Lyger\Http\Request;
use Lyger\Http\Response;

Default Method

Each controller includes an index() method that returns a JSON response:
public function index(): Response
{
    return Response::json(['message' => 'UserController controller']);
}

Naming Conventions

Controller names must follow these rules:
  • Use PascalCase (e.g., UserController, ProductController)
  • Start with a letter
  • Only contain alphanumeric characters and underscores
  • Conventionally end with Controller suffix

Valid Names

php rawr make:controller UserController
php rawr make:controller ProductController
php rawr make:controller Api_UserController
php rawr make:controller DashboardController

Invalid Names

php rawr make:controller user-controller  # Hyphens not allowed
php rawr make:controller 1UserController  # Cannot start with number
php rawr make:controller User Controller  # Spaces not allowed

Validation

The command validates the controller name:
rawr (lines 187-190)
if (!preg_match('/^[A-Za-z][A-Za-z0-9_]*$/', $name)) {
    echo "Error: Invalid controller name. Use PascalCase\n";
    exit(1);
}

Error Handling

Missing Name

php rawr make:controller
Output:
Error: Controller name required

Duplicate Controller

php rawr make:controller UserController
# Run again
php rawr make:controller UserController
Output:
Error: Controller UserController already exists
The command prevents overwriting existing controllers to protect your code.

Common Patterns

RESTful Controller

Create a controller and add RESTful methods:
App/Controllers/ProductController.php
<?php

declare(strict_types=1);

namespace App\Controllers;

use Lyger\Http\Request;
use Lyger\Http\Response;

class ProductController
{
    // List all products
    public function index(): Response
    {
        return Response::json(['products' => []]);
    }
    
    // Show single product
    public function show(int $id): Response
    {
        return Response::json(['product' => ['id' => $id]]);
    }
    
    // Create new product
    public function store(Request $request): Response
    {
        $data = $request->all();
        return Response::json(['created' => true], 201);
    }
    
    // Update product
    public function update(Request $request, int $id): Response
    {
        $data = $request->all();
        return Response::json(['updated' => true]);
    }
    
    // Delete product
    public function destroy(int $id): Response
    {
        return Response::json(['deleted' => true]);
    }
}

API Controller

Create an API controller with versioning:
App/Controllers/ApiController.php
<?php

declare(strict_types=1);

namespace App\Controllers;

use Lyger\Http\Request;
use Lyger\Http\Response;

class ApiController
{
    protected string $version = 'v1';
    
    public function index(): Response
    {
        return Response::json([
            'version' => $this->version,
            'endpoints' => [
                '/api/users',
                '/api/products',
                '/api/orders'
            ]
        ]);
    }
}

Form Controller

Handle HTML form submissions:
App/Controllers/ContactController.php
<?php

declare(strict_types=1);

namespace App\Controllers;

use Lyger\Http\Request;
use Lyger\Http\Response;

class ContactController
{
    public function index(): Response
    {
        return Response::html('<form>...</form>');
    }
    
    public function submit(Request $request): Response
    {
        $name = $request->input('name');
        $email = $request->input('email');
        $message = $request->input('message');
        
        // Process form data
        
        return Response::json([
            'success' => true,
            'message' => 'Contact form submitted'
        ]);
    }
}

Using Generated Controllers

After creating a controller, register it in your routes:
routes/web.php
<?php

use App\Controllers\UserController;

$router->get('/users', [UserController::class, 'index']);
$router->get('/users/{id}', [UserController::class, 'show']);
$router->post('/users', [UserController::class, 'store']);
See the Routing Guide for complete routing documentation.

Workflow Example

php rawr make:controller OrderController

Source Code

The controller generation logic:
rawr (lines 180-225)
function makeController(string $basePath, ?string $name): void
{
    if ($name === null) {
        echo "Error: Controller name required\n";
        exit(1);
    }

    if (!preg_match('/^[A-Za-z][A-Za-z0-9_]*$/', $name)) {
        echo "Error: Invalid controller name. Use PascalCase\n";
        exit(1);
    }

    $controllersPath = $basePath . '/App/Controllers';
    if (!is_dir($controllersPath)) {
        mkdir($controllersPath, 0755, true);
    }

    $targetPath = $controllersPath . '/' . $name . '.php';

    if (file_exists($targetPath)) {
        echo "Error: Controller {$name} already exists\n";
        exit(1);
    }

    $content = <<<PHP
<?php

declare(strict_types=1);

namespace App\Controllers;

use Lyger\Http\Request;
use Lyger\Http\Response;

class {$name}
{
    public function index(): Response
    {
        return Response::json(['message' => '{$name} controller']);
    }
}
PHP;

    file_put_contents($targetPath, $content);
    echo "Controller created: App/Controllers/{$name}.php\n";
}

Next Steps

Make Model

Create models to interact with database

Routing

Register controller routes

Request Handling

Work with HTTP requests

Responses

Return different response types