Creating and Running a PHPixie Project on macOS with ServBay
PHPixie is a lightweight and high-performance PHP framework designed for rapid web application development. It follows the HMVC (Hierarchical Model-View-Controller) design pattern, offering a clean code structure and efficient processing capabilities. Its simplicity, flexibility, and performance have made it a popular choice among many developers.
Key Features and Advantages of PHPixie
- Lightweight: The core system of PHPixie is minimal and only includes essential components for building applications, resulting in fast startup and execution.
- High Performance: The framework is engineered for efficiency, handling concurrent requests effectively and making it suitable for performance-critical applications.
- Easy to Learn: With a clear and concise API and comprehensive documentation, developers can get up to speed quickly.
- Flexibility: PHPixie is loosely coupled, allowing developers to freely choose and integrate third-party libraries and components to meet project needs.
- Active Community Support: There is a vibrant developer community offering a wealth of third-party extensions and support resources.
PHPixie empowers developers to efficiently build high-quality, high-performance web applications, from small projects to large enterprise-level solutions.
Creating and Running a PHPixie Project with ServBay
This article will walk you through setting up and running a PHPixie project using ServBay's local web development environment. We'll use ServBay's integrated PHP environment, Composer dependency manager, and the "Websites" feature to configure the web server, enabling easy local access and database/cache integration.
Prerequisites
- ServBay is installed and running on macOS. ServBay integrates PHP, Composer, various databases (MySQL, PostgreSQL, MongoDB, Redis, etc.), and the Caddy web server.
- Basic command-line knowledge.
Creating a PHPixie Project
Recommended project directory
ServBay recommends placing all your local website projects in the /Applications/ServBay/www
directory for easier management and to comply with ServBay's access control.
- Confirm Composer Installation: ServBay comes pre-installed with Composer—no additional installation needed. Open your terminal and run
composer --version
to verify. - Create Project Directory: In ServBay's recommended root directory, create a new folder for your PHPixie project and navigate into it:bash
cd /Applications/ServBay/www mkdir servbay-phpixie-app cd servbay-phpixie-app
1
2
3 - Create PHPixie Project with Composer: Run Composer to download the PHPixie project template into your project directory:bashComposer will fetch PHPixie and its dependencies into the current directory.
composer create-project phpixie/project .
1
Configuring the Web Server
Set up the Caddy web server using ServBay's Websites feature, ensuring it properly points to and runs your PHPixie project.
Open the ServBay application interface.
Navigate to the Websites list.
Click to add a new website.
Fill in the website configuration:
- Name: For example,
My First PHPixie Dev Site
(This is an internal identifier within ServBay). - Domain: For example,
servbay-phpixie-test.local
(the local development domain you'll use in your browser). - Website Type: Select
PHP
. - PHP Version: Choose your desired PHP version, e.g.,
8.3
. - Document Root: Point to your project's
web
directory:/Applications/ServBay/www/servbay-phpixie-app/web
.
Why the
/web
directory?The entry point for a PHPixie project is
web/index.php
. For security and best practices, the web server's root should point directly to theweb
directory, not the top-level project directory. This prevents sensitive files (like configuration files and the vendor directory) from being directly accessed via the web.- Name: For example,
Save the configuration. ServBay will automatically set up Caddy and update your local hosts file (if necessary), ensuring
servbay-phpixie-test.local
routes to your local ServBay server.
For more detailed steps, see Adding Your First Website.
Adding Example Code
Let's add some sample code to verify your project runs correctly and demonstrate database/cache integration.
Create Example Controller: Add the following code to
src/App/HTTP/Controller/Home.php
. Manually create the file and directories if they don't exist.php<?php namespace App\HTTP\Controller; use PHPixie\HTTP\Request; use PHPixie\Template; use PHPixie\Database\Query; // Import the Query class class Home extends \PHPixie\Controller { protected $template; public function __construct(Template $template) { $this->template = $template; } // Default home page action public function action_index(Request $request) { // Render the assets/templates/app/home.php template return $this->template->render('app:home'); } // Sample Memcached action public function action_memcached(Request $request) { $cache = $this->components->cache(); $cache->set('key', 'Hello Memcached from ServBay!', 60); // Store data, valid for 60 seconds $value = $cache->get('key'); // Retrieve data return $this->response()->string($value); // Return string response } // Sample Redis action public function action_redis(Request $request) { $redis = $this->components->redis(); $redis->set('key', 'Hello Redis from ServBay!'); // Store data $value = $redis->get('key'); // Retrieve data return $this->response()->string($value); // Return string response } // Sample action to add a user (MySQL/PostgreSQL) public function action_add_user(Request $request) { // Use the database component's query builder $query = $this->components->database()->query(); // Insert new user into the 'users' table $query->insert('users')->data([ 'name' => 'ServBay Demo User', 'email' => 'demo-' . uniqid() . '@servbay.test', // Unique email ])->execute(); // Perform insert return $this->response()->string('User added successfully.'); // Return success message } // Sample action to list users (MySQL/PostgreSQL) public function action_list_users(Request $request) { // Use the database component's query builder $query = $this->components->database()->query(); // Fetch all users from the 'users' table $users = $query->select('*')->from('users')->execute()->fetchAll(); // Execute query and get results return $this->response()->json($users); // Return user list as JSON } }
1
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
56
57
58
59
60
61
62
63
64
65
66
67Create Example Template: Add this code to
assets/templates/app/home.php
. Create the file and directories if needed.php<!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 on ServBay</title> <style> body { font-family: sans-serif; line-height: 1.6; margin: 20px; } h1 { color: #333; } p { margin-bottom: 15px; } code { background-color: #f4f4f4; padding: 2px 5px; border-radius: 4px; } ul { list-style: none; padding: 0; } li { margin-bottom: 10px; } li a { text-decoration: none; color: #007bff; } li a:hover { text-decoration: underline; } </style> </head> <body> <h1>Welcome to PHPixie on ServBay</h1> <p>This page is being generated dynamically by your PHPixie application running on ServBay.</p> <h2>Sample Feature Links:</h2> <ul> <li><a href="/home/memcached">Test Memcached</a></li> <li><a href="/home/redis">Test Redis</a></li> <li><a href="/home/add_user">Add User to Database</a></li> <li><a href="/home/list_users">List Users in Database</a></li> </ul> <p>Please make sure you've completed the corresponding database and cache configuration steps.</p> </body> </html>
1
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
Accessing Your Website
After configuring the ServBay website and adding the sample code, you can visit your designated domain in your browser:
- Open your browser and navigate to
https://servbay-phpixie-test.local
. - You should see the "Welcome to PHPixie on ServBay" page along with some sample feature links.
If you can't access the site, check:
- Is ServBay running?
- Is the site's configuration correct in ServBay—especially domain name, website type, and document root?
- Is your local hosts file resolving
servbay-phpixie-test.local
properly? (ServBay typically manages this automatically.) - Is the selected PHP version enabled in ServBay?
Database and Cache Integration Examples
PHPixie natively supports various databases and caching systems. ServBay provides MySQL, PostgreSQL, Redis, Memcached, and more, so you can easily integrate them into your PHPixie project.
Important Note: Before performing database operations, be sure the corresponding database services (e.g., MySQL or PostgreSQL) are running in ServBay, and you've created a database for your project. For this example, the database is named servbay_phpixie_app
. You can use ServBay's database management tools (Sequel Ace, Postico, TablePlus, etc.) or a command-line client to create your database. The default database user is typically root
and the password is password
(verify with your ServBay setup).
Configuring Database Connection
Depending on your chosen database type (MySQL or PostgreSQL), edit the assets/config/database.php
file at the project root. ServBay's default database address is usually 127.0.0.1
.
MySQL Example Configuration (assets/config/database.php
):
php
<?php
return [
'default' => [
'driver' => 'pdo',
// Adjust connection string as needed for your database type and ServBay setup
'connection' => 'mysql:host=127.0.0.1;dbname=servbay_phpixie_app',
'user' => 'root', // ServBay default user
'password' => 'password', // ServBay default password (modify if needed)
'options' => []
]
];
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
PostgreSQL Example Configuration (assets/config/database.php
):
php
<?php
return [
'default' => [
'driver' => 'pdo',
// Adjust connection string as needed for your database type and ServBay setup
'connection' => 'pgsql:host=127.0.0.1;dbname=servbay_phpixie_app',
'user' => 'root', // ServBay default user
'password' => 'password', // ServBay default password (modify if needed)
'options' => []
]
];
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
Creating Database Tables (Using Migrations)
PHPixie offers a CLI tool for managing database migrations—a way to version-control your database schema.
- Open Terminal: Switch to your PHPixie project root:bash
cd /Applications/ServBay/www/servbay-phpixie-app
1 - Create Migration File: Generate a migration file with PHPixie's CLI. We'll create one for the
users
table:bashThis generates a new PHP file inphp pixie generate:migration create_users_table
1assets/migrations
with a timestamped filename. - Edit Migration File: Open the new migration file in
assets/migrations
(e.g.,YYYY_MM_DD_HHMMSS_create_users_table.php
) and define the structure of theusers
table in theup()
anddown()
methods:php<?php use PHPixie\Database\Migration; use PHPixie\Database\Schema\Table; class CreateUsersTable extends Migration { public function up() { // Use schema builder to create the 'users' table $this->schema->create('users', function(Table $table) { $table->increments('id'); // Auto-increment primary key $table->string('name'); // Name column $table->string('email')->unique(); // Email column, unique $table->timestamps(); // created_at and updated_at timestamps }); } public function down() { // Use schema builder to drop the 'users' table $this->schema->drop('users'); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 - Run Migrations: In your project root, execute the migration to create the
users
table:bashIf successful, you'll see a migration confirmation in your terminal. You can use a database management tool to confirm thephp pixie migrate
1users
table was created in theservbay_phpixie_app
database.
Working with Relational Databases (MySQL/PostgreSQL)
In the sample controller src/App/HTTP/Controller/Home.php
, we've already included the action_add_user
and action_list_users
methods to demonstrate interactions with MySQL or PostgreSQL using PHPixie's database component.
- Add a User: Visit
https://servbay-phpixie-test.local/home/add_user
to insert a new record into theusers
table. - List Users: Visit
https://servbay-phpixie-test.local/home/list_users
to fetch all user records and see them returned as JSON.
Configuring and Using NoSQL Databases (Memcached/Redis)
ServBay comes pre-installed with Memcached and Redis as well as their PHP extensions. Just configure them in your PHPixie project to use them.
Memcached Configuration (assets/config/cache.php
):
Edit or create the assets/config/cache.php
file and add Memcached settings:
php
<?php
return [
'default' => [
'driver' => 'memcached',
'servers' => [
[
'host' => '127.0.0.1', // ServBay Memcached default address
'port' => 11211, // ServBay Memcached default port
'weight' => 100,
],
],
],
];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
Redis Configuration (assets/config/redis.php
):
Edit or create the assets/config/redis.php
file and add Redis settings:
php
<?php
return [
'default' => [
'hostname' => '127.0.0.1', // ServBay Redis default address
'port' => 6379, // ServBay Redis default port
'timeout' => 0,
'database' => 0, // Redis database index
],
];
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
Using Memcached/Redis:
The action_memcached
and action_redis
methods in src/App/HTTP/Controller/Home.php
demonstrate PHPixie's cache component usage (configured to use either Memcached or Redis).
- Test Memcached: Visit
https://servbay-phpixie-test.local/home/memcached
to write a key-value pair to Memcached, read it back, and display the result. - Test Redis: Visit
https://servbay-phpixie-test.local/home/redis
to write a key-value pair to Redis, read it back, and display the result.
Ensure that ServBay's Memcached and Redis services are running.
Summary
By following the steps above, you've successfully created, configured, and run a PHPixie project in your local macOS environment using ServBay. You learned how to use Composer to create your project, configure the web server with ServBay's Websites feature, set up basic database and cache integration, and verify these features work as expected.
ServBay's integrated environment dramatically simplifies the setup process for PHP development, allowing you to focus on coding. We hope this guide helps you quickly start using PHPixie for your next web project!