A Php Router with a custom error handler
- Supports all request methods like POST, GET, PUT, DELETE
- Can show your custom error pages
- Support Error suppression using @ symbol
- Supports url parameters for dynamic urls
- Works by calling specific functions from specific class. So no need to create multiple files for multiple requests.
Download this codes extract them to your desire folder. Custimize the namespaces as your environment. If you have autoloader you can remove the require_once statement in line 3 and 59 in the Router.php file. Also specify your Controllers folder path and namespace in line 56 and 60 in the Router.php file.
Configure your .htaccess for the PhpRouter. Copy paste the codes below to your .htaccess file.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
-
If you don't have a autoloader require the ErrorHandler.php file
require_once(__DIR__.'/Services/ErrorHandler.php');
-
Set the error handler
use Services/ErrorHandler; ErrorHandler::init();
- Add use statemate for the Router class
use Services\Router;
- Add require statement if there is no autoloader
require_once(__DIR__."/services/Router.php"); //Customize the path as your file structure
- Create Router instance
new Router;
-
For GET requests
Router::get("/example", "PageController@loadExample");//PageController is a class and the loadExample is a function inside that
//Here is the example class class PageController{ public function loadExample(){ //do anything you want here include('example.php');//For example } }
-
For GET requests with dynamic urls
Router::get("/product/{id}", "ProductController@loadProduct");//You can directly access the id in the loadProduct function as the code below
//Here is the example class class ProductController{ public function loadProduct($id){ echo 'The product id is: '. $id; } }
-
For POST requests
Router::post("/submit-form", "FormController@submit");//You can access the POST data inside the submit function normally
-
For PUT requests
Router::put("/update-product", "ProcuctController@updateProduct");//You can access the PUT data inside the submit function from the php input
-
For DELETE requests
Router::del("/delete-product", "ProcuctController@deleteProduct");//You can access the DELETE data inside the submit function from the php input
Router::dispatch();
You can also use the custome error handler to show error from anywhere from your code. Just require ErrorHandler.php file if you don't have a autoloader. If you have a autoloader just add a use statement to your file and show a error page like below.
ErrorHandler::showErrorPage(404);
You can also create your own error code and a error page for that. Check near line 29 in the ErrorHandler.php file. If you wanna use a custom error code, use something after 600 to avoid collision with default error codes.
NOTE: Please leave a star if you find it valuable. And please let me know where to improve