HasPHP is a high-performance PHP framework built on OpenSwoole, designed for building scalable and efficient web applications and APIs. It comes with built-in support for routing, database operations, and template rendering using Twig.
- 🚀 Built on OpenSwoole for high performance
- 🛣️ Simple and intuitive routing system
- 🗃️ Database abstraction with multiple driver support (SQLite included)
- 📝 Template engine support with Twig
- 🔐 JWT authentication support
- 🛠️ Command-line interface for common tasks
- 📦 PSR-4 autoloading
- PHP 8.0 or higher
- OpenSwoole extension
- Composer
-
Clone the repository:
git clone https://github.com/your-username/hasphp.git cd hasphp
-
Install dependencies:
composer install
-
Set up the database (SQLite by default):
mkdir -p database touch database/database.sqlite
hasphp/
├── app/ # Application code
│ ├── Controllers/ # Controller classes
│ ├── Core/ # Framework core
│ │ ├── DB/ # Database components
│ │ │ └── Drivers/ # Database drivers
│ │ ├── Middleware/ # Middleware classes
│ │ └── Internal/ # Internal framework classes
├── cli/ # Command-line tools
│ └── Commands/ # CLI command classes
├── database/ # Database files and migrations
│ └── migrations/ # Database migration files
├── routes/ # Route definitions
├── views/ # Template files
├── composer.json # Composer configuration
└── Readme.md # This file
Define routes in routes/web.php
:
use Hasphp\App\Core\Router;
// Simple GET route
Router::get('/', 'HomeController@index');
// Route with OpenAPI documentation
Router::get('/hello', 'HomeController@hello', [], [
'tags' => ['Home'],
'summary' => 'Say Hello',
'description' => 'Returns a plain text greeting',
'responses' => [
200 => ['type' => 'string']
]
]);
// POST route with request body validation
Router::post('/login', 'AuthController@login', [], [
'tags' => ['Auth'],
'summary' => 'User login',
'description' => 'Authenticate user and return a token'
]);
Example model usage:
use Hasphp\App\Core\DB\Drivers\SQLiteDriver;
// Initialize database connection
$db = new SQLiteDriver();
// Execute a query
$stmt = $db->pdo()->query('SELECT * FROM users');
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Execute a prepared statement
$stmt = $db->pdo()->prepare('INSERT INTO users (name, email) VALUES (?, ?)');
$stmt->execute(['John Doe', '[email protected]']);
Start the OpenSwoole server:
php public/index.php
The application will be available at http://localhost:9501
by default.
Edit the database connection settings in app/Core/DB/Drivers/SQLiteDriver.php
for SQLite, or create a new driver class for other databases.
Create a .env
file in the root directory with your environment-specific settings:
APP_ENV=development
APP_DEBUG=true
DB_CONNECTION=sqlite
DB_DATABASE=/path/to/your/database.sqlite
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- OpenSwoole for the high-performance coroutine server
- Twig for template engine
- Firebase JWT for authentication