Xpress is a versatile PHP library designed to streamline the creation of API endpoints, drawing inspiration from the simplicity and elegance of Express.js
, a popular framework in the Node.js
ecosystem. This library, currently in its beta stage, empowers developers to handle HTTP GET and POST requests with ease.
- PHP >= 5.1
Currently you can simply download this repository and start using it. No additional configuration required.
<?php
/*This is index.php file*/
include __DIR__ .'/src/xpress.php';
APP::get('/', function($req, $res){
$res->send('Xpress is live! 🥳🥳');
});
APP::end();
APP::get('/user/:id', function($req, $res){
$res->status(200)->json(array('userId'=> $req['id']));
});
In order to use external file to route request and manage from another php file, you have to create a rout in index.php
file. In this example it'll be in data
dir i.e data/vehicle.php
.
<?php
/*This is index.php file*/
include __DIR__ .'/src/xpress.php';
/*Including external file to routs request of vehicle endpoits*/
APP::use('/vehicle', 'data/vehicle');
APP::end();
<?php
/*This is vehicle.php file*/
$vehicleList = array('Tata Nexon', 'Kia Seltos', 'Hyundai Creta', 'Hyundai Exter', 'Mahindra Thar');
/*Exmaple 1 | With user defined status code | URL: http://localhost/vehicle*/
APP::get('/', function($req, $res){
$res->status(200)->send('Hello! from vehicle.php file.');
});
/*Example 2 | With app defined status code | URL: http://localhost/vehicle/list*/
APP::get('/list', function($req, $res) use ($vehicleList){
$res->json($vehicleList);
});
/*Exmaple 3 | With virtual prefix | URL: http://localhost/vehicle/name/1*/
APP::get('/name/:index', function($req, $res) use ($vehicleList){
$res->send($vehicleList[$req['index']]);
});
APP::end();
You can redirect a virtual path to any other endpoint, with status code & params. Here true
is to include all params else you can pass an array of keys to be include in redirect URI.
APP::redirect("/github", "https://github.com/cttricks", 302, true);
Or you can also include virtual prefix & use that as params in target URL
APP::redirect("/github/:tab", "https://github.com/cttricks", 302, ['tab']);
Or keep it simeple & redirect without params
APP::redirect("/github", "https://github.com/cttricks");
Contributions are welcome! If you think you can improve the performance, add more animations, or enhance the effects of xpress, I encourage you to contribute to the project.
To contribute, please follow these steps:
-
Fork the repository on GitHub.
-
Create a new branch with a descriptive name for your contribution.
-
Make your modifications, improvements, or additions to the codebase.
-
Test your changes to ensure they work as expected.
-
Commit your changes and push them to your forked repository.
-
Submit a pull request from your branch to the main repository's
master
branch.
I appreciate your contributions and will review your pull request as soon as possible. Feel free to provide any additional context, explanations, or documentation related to your contribution in the pull request description.
💡 Please note that by contributing to this repo, you agree to release your contributions under the Apache-2.0 License.
Xpress is released under the Apache-2.0 License. See the LICENSE file for more details.
This project is a fun experiment and should not be used in a production environment without proper testing and security measures. Use it responsibly and at your own risk.
Feel free to reach out if you have any questions or encounter issues during setup. Happy coding!