PHPackages                             giovanni-venturelli/sophia - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. giovanni-venturelli/sophia

ActiveLibrary[Framework](/categories/framework)

giovanni-venturelli/sophia
==========================

Sophia is a lightweight component-based PHP framework with native PHP templates.

v0.1.47(1mo ago)160MITPHPPHP &gt;=8.1

Since Dec 27Pushed 1mo agoCompare

[ Source](https://github.com/giovanni-venturelli/sophia)[ Packagist](https://packagist.org/packages/giovanni-venturelli/sophia)[ RSS](/packages/giovanni-venturelli-sophia/feed)WikiDiscussions main Synced today

READMEChangelog (4)Dependencies (2)Versions (49)Used By (0)

Sophia Framework — Lightweight PHP Components, DI, and Router
=============================================================

[](#sophia-framework--lightweight-php-components-di-and-router)

Minimal, production‑oriented PHP framework with:

- Component-based rendering with native PHP templates
- Angular‑inspired Dependency Injection system
- Simple Router (components, API callbacks, parameters, guards)
- Optional database layer with fluent QueryBuilder and Active Record ORM

Components are plain PHP classes (annotated), services are auto‑wired, routes map to components (views) or callbacks (APIs).

Quick navigation
----------------

[](#quick-navigation)

- [What you get (features)](#what-you-get-features)
- [Architecture at a glance](#architecture-at-a-glance)
- [Installation](#installation)
- [Project structure](#project-structure)
- [Quick start (index.php + routes.php)](#quick-start-indexphp--routesphp)
- [Create your first component](#create-your-first-component)
- [Dependency Injection (services)](#dependency-injection-services)
- [Routing basics (components, params, urls)](#routing-basics-components-params-urls)
- [Performance &amp; Build System](#performance--build-system)
- [Database integration (optional)](#database-integration-optional)
- [Troubleshooting](#troubleshooting)
- [Deep dives (module READMEs)](#deep-dives-module-readmes)
- [Migration from Twig to native PHP](#migration-from-twig-to-native-php)

What you get (features)
-----------------------

[](#what-you-get-features)

- Components rendered with native PHP templates and helper functions
- Property injection in components and constructor injection in services
- Root singletons via `#[Injectable(providedIn: 'root')]`
- Route configuration with parameters, named routes, redirects, nested routes, guards
- Optional database service with fluent QueryBuilder and Active Record entities

Architecture at a glance
------------------------

[](#architecture-at-a-glance)

- Components: `core/component` (attributes, registry, renderer)
- DI (Injector): `core/injector` (root singletons + per‑component scoped providers)
- Router: `core/router` (maps requests to components or callbacks)
- Database (optional): `core/database` (connection service + ORM)

Flow for a component route:

1. `Router` matches the incoming path → selects a component class
2. `ComponentRegistry` lazily registers it and `Renderer` creates a `ComponentProxy`
3. `ComponentProxy` opens a DI scope, warms providers, creates the component, runs property injection, then `onInit()`
4. `Renderer` collects public properties/zero‑arg getters and renders the PHP template

Installation
------------

[](#installation)

- PHP 8.1+
- Composer

```
composer install
```

If you use environment variables, add a `.env` file (Dotenv is included in composer.json) and configure the database as needed (see [Database integration](#database-integration-optional)).

Project structure
-----------------

[](#project-structure)

```
.
├─ core/
│  ├─ component/     # Component system (attributes, registry, renderer)
│  ├─ injector/      # DI container + attributes
│  ├─ router/        # Router + middleware
│  └─ database/      # Optional DB service + ORM
├─ pages/            # Your component classes and PHP templates
├─ routes.php        # Route table
├─ index.php         # App bootstrap
└─ vendor/           # Composer dependencies

```

Quick start (index.php + routes.php)
------------------------------------

[](#quick-start-indexphp--routesphp)

Minimal bootstrap in `index.php`:

```
