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'];
}