Creating and Running a PHPixie Project
What is PHPixie?
PHPixie is a lightweight PHP framework designed for rapid development of high-performance web applications. It follows the HMVC (Hierarchical Model-View-Controller) design pattern, providing a clean code structure and high performance. PHPixie is known for its simplicity, flexibility, and high performance, making it a popular choice among developers.
Main Features and Benefits of PHPixie
- Lightweight: PHPixie's core system is very small, containing only essential components, and is extremely fast to load.
- High Performance: Known for its efficient performance and speed, PHPixie can handle high concurrent requests.
- Easy to Learn: With a simple and easy-to-use API and comprehensive documentation, developers can quickly get started.
- Flexibility: It allows developers to choose and use third-party libraries and plugins freely, making it easy to extend and customize functionalities.
- Strong Community Support: PHPixie has an active developer community and a wealth of third-party extensions.
PHPixie can help developers quickly build high-performance, high-quality web applications suitable for projects of all sizes.
Creating and Running a PHPixie Project Using ServBay
In this article, we'll create and run a PHPixie project using the PHP environment provided by ServBay. We'll use ServBay's 'Host' feature to set up the web server and configure project access with minimal effort.
Note: If you've been a user of NGINX or Apache
ServBay uses Caddy as its default web server. If you're transitioning from NGINX or Apache to ServBay, there are some key changes to note:
Caddy Configuration
ServBay comes with Caddy pre-installed, and the default configuration has been optimized and debugged. Developers can manage sites through ServBay’s 'Host' feature without manually modifying the Caddy configuration file.
Rewrite Rules and .htaccess
In NGINX and Apache, developers typically write their own rewrite rules and .htaccess files to handle URL rewriting and other configurations. However, ServBay comes with pre-configured Caddy rules, so you don't need to write these rules yourself unless you have specific requirements.
Learn More
For more information, please refer to Rewrite and htaccess, How to Migrate Apache Sites to ServBay, and How to Migrate NGINX Sites to ServBay.
Creating a PHPixie Project
TIP
ServBay recommends developers place websites in the /Applications/ServBay/www
directory for easier management.
Install Composer
Composer comes pre-installed with ServBay, so there's no need to install it separately.
Create a PHPixie Project
Use Composer to create a new PHPixie project:
bashcd /Applications/ServBay/www mkdir servbay-phpixie-app cd servbay-phpixie-app composer create-project phpixie/project .
1
2
3
4Enter the Project Directory
Navigate to the newly created PHPixie project directory:
bashcd /Applications/ServBay/www/servbay-phpixie-app
1
Initial Configuration
Configure Database Connection
In the
assets/config/database.php
file, configure the database connection information:phpreturn [ 'default' => [ 'driver' => 'pdo', 'connection' => 'mysql:host=127.0.0.1;dbname=servbay_phpixie_app', 'user' => 'root', 'password' => 'password', 'options' => [] ] ];
1
2
3
4
5
6
7
8
9
Configuring the Web Server
Use ServBay's 'Host' feature to access the PHPixie project via the web server. In ServBay's 'Host' settings, add a new host:
- Name:
My First PHPixie Dev Site
- Domain:
servbay-phpixie-test.local
- Site Type:
PHP
- PHP Version: Select
8.3
- Root Directory:
/Applications/ServBay/www/servbay-phpixie-app/web
For detailed setup steps, please refer to Adding Your First Site.
Adding Sample Code
In the src/App/HTTP/Controller/Home.php
file, add the following code:
namespace App\HTTP\Controller;
use PHPixie\HTTP\Request;
use PHPixie\Template;
class Home extends \PHPixie\Controller
{
protected $template;
public function __construct(Template $template)
{
$this->template = $template;
}
public function action_index(Request $request)
{
return $this->template->render('app:home');
}
public function action_memcached(Request $request)
{
$cache = $this->components->cache();
$cache->set('key', 'Hello Memcached!', 60);
$value = $cache->get('key');
return $this->response()->string($value);
}
public function action_redis(Request $request)
{
$redis = $this->components->redis();
$redis->set('key', 'Hello Redis!');
$value = $redis->get('key');
return $this->response()->string($value);
}
public function action_mysql_add(Request $request)
{
$query = $this->components->database()->query();
$query->insert('users')->data([
'name' => 'ServBay',
'email' => '[email protected]',
])->execute();
return $this->response()->string('User added');
}
public function action_mysql(Request $request)
{
$query = $this->components->database()->query();
$users = $query->select('*')->from('users')->execute()->fetchAll();
return $this->response()->json($users);
}
}
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
In the assets/templates/app/home.php
file, add the following code:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to PHPixie</title>
</head>
<body>
<h1>Welcome to PHPixie</h1>
<p>The page you are looking at is being generated dynamically by PHPixie.</p>
</body>
</html>
2
3
4
5
6
7
8
9
10
11
12
Accessing the Site
Open your browser and visit the following URL:
https://servbay-phpixie-test.local
: You should see the page displayingWelcome to PHPixie
.
NoSQL Database Examples
Memcached Example
Install Memcached Extension
The Memcached extension is pre-installed in ServBay, so no extra installation is necessary.
Configure Memcached
In the
assets/config/cache.php
file, configure the Memcached connection information:phpreturn [ 'default' => [ 'driver' => 'memcached', 'servers' => [ [ 'host' => '127.0.0.1', 'port' => 11211, 'weight' => 100, ], ], ], ];
1
2
3
4
5
6
7
8
9
10
11
12Use Memcached
Use cache in the controller:
phppublic function action_memcached(Request $request) { $cache = $this->components->cache(); $cache->set('key', 'Hello Memcached!', 60); $value = $cache->get('key'); return $this->response()->string($value); }
1
2
3
4
5
6
7
Redis Example
Install Redis Extension
The Redis extension is pre-installed in ServBay, so no extra installation is necessary.
Configure Redis
In the
assets/config/redis.php
file, configure the Redis connection information:phpreturn [ 'default' => [ 'hostname' => '127.0.0.1', 'port' => 6379, 'timeout' => 0, 'database' => 0, ], ];
1
2
3
4
5
6
7
8Use Redis
Use cache in the controller:
phppublic function action_redis(Request $request) { $redis = $this->components->redis(); $redis->set('key', 'Hello Redis!'); $value = $redis->get('key'); return $this->response()->string($value); }
1
2
3
4
5
6
7
Relational Database Examples
Creating Database Structure and Migration Files
Create Migration File
Use PHPixie's CLI tool to create a migration file:
bashphp pixie generate:migration create_users_table
1Edit Migration File
In the
assets/migrations
directory, locate the newly created migration file and edit it to define the database table structure:phppublic function up() { $this->schema->create('users', function($table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->timestamps(); }); } public function down() { $this->schema->drop('users'); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14Run Migration
Use PHPixie's CLI tool to run the migration and create the database table:
bashphp pixie migrate
1
MySQL Example
Configure MySQL
In the
assets/config/database.php
file, configure the MySQL connection information:phpreturn [ 'default' => [ 'driver' => 'pdo', 'connection' => 'mysql:host=127.0.0.1;dbname=servbay_phpixie_app', 'user' => 'root', 'password' => 'password', 'options' => [] ] ];
1
2
3
4
5
6
7
8
9Insert User Data
Use the controller to insert user data:
phppublic function action_mysql_add(Request $request) { $query = $this->components->database()->query(); $query->insert('users')->data([ 'name' => 'ServBay', 'email' => '[email protected]', ])->execute(); return $this->response()->string('User added'); }
1
2
3
4
5
6
7
8
9Use MySQL
Call the database in the controller:
phppublic function action_mysql(Request $request) { $query = $this->components->database()->query(); $users = $query->select('*')->from('users')->execute()->fetchAll(); return $this->response()->json($users); }
1
2
3
4
5
6
PostgreSQL Example
Configure PostgreSQL
In the
assets/config/database.php
file, configure the PostgreSQL connection information:phpreturn [ 'default' => [ 'driver' => 'pdo', 'connection' => 'pgsql:host=127.0.0.1;dbname=servbay_phpixie_app', 'user' => 'root', 'password' => 'password', 'options' => [] ] ];
1
2
3
4
5
6
7
8
9Insert User Data
Use the controller to insert user data:
phppublic function action_pgsql_add(Request $request) { $query = $this->components->database()->query(); $query->insert('users')->data([ 'name' => 'ServBay', 'email' => '[email protected]', ])->execute(); return $this->response()->string('User added'); }
1
2
3
4
5
6
7
8
9Use PostgreSQL
Call the database in the controller:
phppublic function action_pgsql(Request $request) { $query = $this->components->database()->query(); $users = $query->select('*')->from('users')->execute()->fetchAll(); return $this->response()->json($users); }
1
2
3
4
5
6
By following the above steps, you have successfully created and run a PHPixie project, managed and accessed the project using ServBay's functionalities, and connected to multiple databases and called data. We hope this article helps you get started with PHPixie and apply it to your projects.