Skip to content

Backend: PHP router, CORS config, and API entry point #33

@Manak-hash

Description

@Manak-hash

Description

Build the PHP routing layer that maps HTTP method + path to controllers, handles CORS for the React frontend, and sets up global error handling. This is the backbone of the entire backend.

Tasks

  • Create public/index.php as the single entry point (front controller)
  • Configure .htaccess in public/ to rewrite all requests to index.php
  • Build a minimal router class in helpers/Router.php:
    • Register routes with method + path + controller callback
    • Parse path parameters (e.g. /projects/:id)
    • Return 404 JSON if no route matches
    • Return 405 JSON if method doesn't match
  • Register all API routes in routes/api.php
  • Add CORS headers for local dev (http://localhost:5173) and handle OPTIONS preflight
  • Global exception handler — catch uncaught exceptions and return JSON error
  • Set Content-Type: application/json for all responses
  • Create helpers/Response.php with success($data, $code) and error($message, $code) helpers
  • Test: GET /api/ping returns {"status": "ok"}
  • Test: unknown route returns {"error": "Not found"} with 404

Acceptance Criteria

  • All API requests go through index.php
  • CORS headers allow requests from the React frontend
  • Router correctly maps paths including parameters
  • Unknown routes return clean JSON 404

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions