PHP code example of giovanni-venturelli / sophia

1. Go to this page and download the library: Download giovanni-venturelli/sophia library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

giovanni-venturelli / sophia example snippets



ia\App::create(__DIR__, [
    'basePath' => 'sophia',            // optional, if the app lives in a subfolder
    'language' => 'it',
    'styles'   => ['css/style.css'],   // relative to the base path
    'scripts'  => ['js/scripts.js'],
])->run();


use App\Pages\Home\HomeComponent;

return [
    [
        'path' => 'home/:id',                  // URL with param
        'component' => HomeComponent::class,  // Component class
        'name' => 'home',                     // Named route
        'data' => [ 'title' => 'Home Page' ], // Route-scoped data
    ],
];


namespace App\Pages\Home;

use Sophia\Component\Component;

#[Component(selector: 'app-home', template: 'home.php')]
class HomeComponent
{
    public string $title = 'Welcome';
    public string $id;

    public function onInit(): void
    {
        // Optionally compute public state for the template
    }
}

<h1><?= $e($title) 

use Sophia\Injector\Injectable;
use Sophia\Injector\Inject;
use Sophia\Component\Component;

#[Injectable(providedIn: 'root')]
class Logger { public function info(string $m): void {} }

#[Injectable]
class UserService { public function __construct(private Logger $log) {} }

#[Component(selector: 'app-users', template: 'users.php', providers: [UserService::class])]
class UsersComponent
{
    #[Inject] private UserService $users;
    public array $active = [];

    public function onInit(): void { $this->active = $this->users->getActive(); }
}

<a href="<?= $e($url('home', ['id' => 123])) 

use Sophia\Database\Entity;

class Post extends Entity
{
    protected static string $table = 'posts';
    protected static array $fillable = ['title', 'content', 'status'];
}

$posts = Post::where('status', 'published')->orderBy('created_at', 'DESC')->limit(10)->get();
$one   = Post::find(1);

[
  'path' => 'forms/submit/:token',
  'callback' => [\Sophia\Form\FormController::class, 'handle'],
  'name' => 'forms.submit'
],

<form method="post" action="<?= $e($form_action('send')) 

[
  'path' => 'about',
  'component' => App\Pages\About\AboutLayoutComponent::class,
  'name' => 'about',
  'children' => 

<a href="<?= $e($url('about')) 

.
├─ pages/            # Your component classes and PHP templates
├─ config/
│  └─ database.php   # Optional, picked up automatically
├─ routes.php        # Route table
├─ index.php         # App bootstrap
├─ .env              # Optional, loaded automatically
└─ vendor/           # Composer dependencies (Sophia lives in vendor/giovanni-venturelli/sophia/src)
json
"extra": {
    "sophia": { "components": ["pages"], "routes": "routes.php" }
}
bash
# Generate structural cache (components, routes, DI)
php sophia build

# Clear the build cache
php sophia clear
bash
cd demo
php ../sophia build   # then set DEBUG=false; `php ../sophia clear` to go back
php benchmark.php     # compares debug and production timings