This repository demonstrates a lightweight PHP server implementation. It includes core functionalities for handling HTTP requests, generating responses, and managing autoloading using class and namespace maps.
The PHP Simple Server Project serves as an educational and foundational tool for PHP developers. It offers insights into building a server-side application without relying on heavy frameworks. The project is designed to teach the principles of:
- HTTP request and response handling.
- PHP autoloading mechanisms with namespaces and class maps.
- Building scalable and minimalistic server-side applications.
- Custom Autoloader:
autoload_classmap.php: Maps classes to file paths.autoload_namespace.php: Maps namespaces to directories.ClassLoader.php: Registers the autoloader for seamless class and namespace loading.
- Request Handling:
Request.php: Parses HTTP methods, URIs, headers, and parameters.
- Response Management:
Response.php: Manages HTTP responses, including headers, body content, and status codes.
- Error Handling:
Exception.php: Custom exception handling for the PHPServer namespace.
- Server Logic:
Server.php: Handles incoming HTTP requests and dispatches responses.
In real-world scenarios, this project can serve as a starting point for:
- Learning the fundamentals of server-side development in PHP.
- Prototyping APIs or microservices.
- Customizing a lightweight server framework for specific use cases.
- Educational Value: Simplifies the complexity of frameworks by focusing on core concepts.
- Flexibility: Allows developers to customize and extend the server to meet unique requirements.
- Efficiency: Lightweight design for small-scale projects and APIs.
- Framework Independence: Provides a deeper understanding of PHP's capabilities without external dependencies.
-
Autoloading Complexity:
- Simplified class and namespace autoloading for better organization.
-
Error Logging:
- Enhanced error detection for missing files or invalid inputs.
-
Scalability:
- Modular design for easy extension and integration.
-
Compatibility:
- Fully compatible with PHP 7.4 and later (including PHP 8.x).
- Clone this repository:
git clone https://github.com/Shahad-irl/simple-php-server.git
cd my-project- Navigate the project directory:
cd simple-php-server- Start the PHP built-in server:
php -S localhost:8000- Access the server in your browser at
http://localhost:8000
- Set up the autoloader
- Configure
autoload_classmap.phpandautoload_namespace.phpto match your class and namespace structure.
- Create a server instance:
require_once 'ClassLoader.php';
$classLoader = new ClassLoader();
$classLoader->addClassMap(require 'autoload_classmap.php');
$classLoader->addNamespaceMap(require 'autoload_namespace.php');
$classLoader->register();
$server = new Server();
$server->handleRequest();Making a request to the server root(http://localhost:8000) will return:
{
"message": "Hello, world!"
}
Contributions are always welcome! If you have ideas for improvements or additional features, feel free to fork the repository and submit a pull request.