Come abilitare il modulo Phalcon integrato in ServBay
Come potente strumento integrato di sviluppo web, ServBay include il modulo Phalcon, il cui processo di attivazione è estremamente semplice. Phalcon, come un framework PHP ad alte prestazioni, ha un design modulare che permette agli sviluppatori di costruire applicazioni web efficienti con facilità.
Introduzione al framework Phalcon
Phalcon è un framework PHP open source a funzionalità complete, noto per la sua velocità e il basso consumo di risorse. Diversamente dai framework PHP tradizionali, Phalcon è scritto in linguaggio C ed è caricato come un'estensione PHP. Questo conferisce a Phalcon un notevole vantaggio in termini di prestazioni, offrendo al contempo una ricca serie di funzionalità.
Caratteristiche principali
- Alte prestazioni: Siccome Phalcon è eseguito come estensione C, è più veloce dei framework implementati in puro PHP e consuma meno risorse.
- Basso overhead: Il design architetturale di Phalcon consente di gestire le richieste utilizzando meno memoria e risorse CPU.
- Ricche funzionalità: Phalcon offre una struttura MVC completa, ORM, motore di template, routing, caching, code e altre funzionalità per soddisfare diverse esigenze di sviluppo web.
- Facile da usare: Anche se Phalcon è scritto in C, gli sviluppatori possono usare la sintassi PHP familiare per sviluppare, riducendo i costi di apprendimento.
- Design modulare: I componenti di Phalcon sono altamente modulari, permettendo agli sviluppatori di selezionare e utilizzare diversi componenti a seconda delle necessità.
Versioni del modulo Phalcon integrato in ServBay
ServBay supporta diverse versioni di PHP e preinstalla i moduli Phalcon corrispondenti per ognuna di esse. Le versioni specifiche sono:
- PHP 5.6, 7.0, 7.1: Phalcon 3.4.5
- PHP 7.2, 7.3, 7.4: Phalcon 4.1.2
- PHP 8.0, 8.1, 8.2, 8.3, 8.4: Phalcon 5.7.0
Come abilitare il modulo Phalcon
Per impostazione predefinita, il modulo Phalcon è disabilitato. Attivarlo è semplicissimo, basta modificare il file di configurazione della versione di PHP corrispondente. Ecco i passaggi dettagliati:
Passo 1: Trova il file di configurazione
Prima, individua la directory conf.d
della versione PHP corrispondente. Ad esempio, per abilitare il modulo Phalcon su PHP 8.3, è necessario modificare il seguente file:
/Applications/ServBay/etc/php/8.3/conf.d/phalcon.ini
Passo 2: Modifica il file di configurazione
Apri il file phalcon.ini
e decommenta la seguente riga:
[Phalcon]
; Decommenta la seguente riga per abilitare Phalcon
extension = phalcon.so
2
3
Passo 3: Abilita il modulo PSR (solo per PHP 7.2 - PHP 7.4)
Per le versioni PHP 7.2 a PHP 7.4, è necessario caricare anche il modulo PSR (incluso in ServBay):
; Decommenta sia Psr che Phalcon per abilitare Phalcon
[Psr]
extension = psr.so
[Phalcon]
extension = phalcon.so
2
3
4
5
Passo 4: Riavvia il servizio PHP
Nel pannello di gestione dei servizi di ServBay, riavvia il servizio PHP corrispondente. Ad esempio, riavvia il servizio PHP 8.3. Una volta completato il riavvio, il modulo Phalcon sarà correttamente caricato.
Verifica del caricamento del modulo Phalcon
Puoi verificare se il modulo Phalcon è stato caricato correttamente creando un semplice file PHP. Crea un file phpinfo.php
nella directory radice del server web con il seguente contenuto:
<?php
phpinfo();
?>
2
3
Accedi a https://servbay.host/phpinfo.php
e cerca le informazioni relative al modulo Phalcon nella pagina di informazioni PHP. Se trovi le informazioni relative a Phalcon, significa che il modulo è stato caricato con successo.
Esempio d'uso
Dopo aver abilitato il modulo Phalcon, puoi usare il framework Phalcon nel codice PHP per lo sviluppo web. Ecco un semplice esempio che mostra come creare una base per un'applicazione web con Phalcon:
Esempio di codice
- Crea la struttura della directory del progetto
phalcon
├── app
│ ├── controllers
│ │ └── IndexController.php
│ └── views
│ └── index
│ └── index.phtml
└── public
└── index.php
2
3
4
5
6
7
8
9
- Controller frontend: public/index.php
<?php
use Phalcon\Autoload\Loader;
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Url;
use Phalcon\Mvc\Application;
define('BASE_PATH', dirname(__DIR__ . '/../../'));
define('APP_PATH', BASE_PATH . '/app');
$loader = new Loader();
$loader->setDirectories(
[
APP_PATH . '/controllers/',
APP_PATH . '/models/',
]
);
$loader->register();
$container = new FactoryDefault();
$container->set(
'view',
function () {
$view = new View();
$view->setViewsDir(APP_PATH . '/views/');
return $view;
}
);
$container->set(
'url',
function () {
$url = new Url();
$url->setBaseUri('/');
return $url;
}
);
$application = new Application($container);
try {
// Gestisci la richiesta
$response = $application->handle(
$_SERVER["REQUEST_URI"]
);
$response->send();
} catch (\Exception $e) {
echo 'Exception: ', $e->getMessage();
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
- Controller: app/controllers/IndexController.php
<?php
use Phalcon\Mvc\Controller;
use Phalcon\Support\Version;
class IndexController extends Controller
{
public function indexAction()
{
// Ottieni la versione di Phalcon
$phalconVersion = new Version();
// Passa la versione alla vista
$this->view->phalconVersion = $phalconVersion->get();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- Vista: app/views/index/index.phtml
<!DOCTYPE html>
<html>
<head>
<title>Hello ServBay</title>
</head>
<body>
<h1>Hello ServBay!</h1>
<p>Phalcon Version: <?= $phalconVersion ?></p>
</body>
</html>
2
3
4
5
6
7
8
9
10
Conclusione
ServBay offre un modo conveniente per gestire e abilitare il modulo Phalcon. Con semplici configurazioni e operazioni di riavvio, gli sviluppatori possono rapidamente abilitare il modulo Phalcon nelle diverse versioni di PHP, sfruttando le sue alte prestazioni e le ricche funzionalità per migliorare l'efficienza nello sviluppo di applicazioni web. Le alte prestazioni, il basso overhead e le ricche funzionalità di Phalcon lo rendono la scelta ideale per costruire applicazioni web efficienti e scalabili.