, (*1)
, (*2)
:tractor: Robust PHP router :tractor:
, (*3)
:fire: Introduction :
Aven (ayven) is a robust and flexible PHP router For PHP7 and newer versions., (*4)
:ok_hand: Features :
- Flexibility (Route calling as a
Facade
or as an Object
).
- More than 14
HTTP VERBS
GET
,POST
,PUT
,PATCH
,DELETE
,OPTION
,PURGE
,HEAD
,COPY
- Name it what ever you want
Aven
,MyRouter
,Banana
,DonaldTrump
.
- Regular Expressions Filters
filter()
.
-
Aven
CLI (Command Line Tool).
- Routes Listing.
- Routes caching to speed up your application on production.
- callback call, controller method call, static method call.
- Rerun data formating (arrays and objects are encoded to json by default).
:pushpin: Requirements :
- PHP 7.2 or newer versions
- PHPUnit >= 8 (for testing purpose)
:rocket: Installation & Use :
composer require lotfio/aven
:wrench: Configuration:
You should redirect all requests to a front page index.php
for example., (*5)
APACHE :, (*6)
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
NGINX :, (*7)
location / {
try_files $uri /$uri /index.php?$query_string;
}
:pencil2: Usage :
1- Quick use : use Aven Facade
, (*8)
<?php
use Aven\Facades\Aven;
// autoload composer
require_once 'vendor/autoload.php';
// get with a callback
Aven::get("/", function(){
return "Hello from Aven this is base route";
});
Aven::init(); // initialize router
Note :, (*9)
Don't forget to Aven::init()
initialize the router, if you are using the router in your custom framework you can move the Aven::init()
statment to the Kernel to clean up your routes file., (*10)
- With a controller method :
// get with a controller method
Aven::get("/index", "IndexController@indexMethod");
- With a static controller method :
// get with a controller static method
Aven::get("/index", "IndexController::indexMethod");
- If you use namespaces you can prepend the controller name or use
Aven::config
to set the base namespace :
// get with a controller method
Aven::get("/index", "YourNamespace\IndexController@indexMethod");
Aven::config([
"namespace" => "Your\\Namespace\\"
]);
Available HTTP
verbs :, (*11)
GET
,POST
,PUT
,PATCH
,DELETE
,COPY
, HEAD
,OPTIONS
,LINK
,UNLINK
,PURGE
,
LOCK
,UNLOCK
,PROPFIND
,ANY
, (*12)
Note :, (*13)
As HTML supports only the GET
and POST
from methods it is handy to append a hidden input to your form named _method
to be able to use other HTTP METHODS
and to surpass this limitation :, (*14)
<form action="" method="POST">
<input type="hidden" name="_method" value="PUT">
</form>
Route parameters :, (*15)
Aven::get("/users/{id}", function($id){
echo " get users by id " . $id;
});
Route parameters filters:, (*16)
You can attach a filter()
method to your routes, (*17)
Aven::get("/users/{id}", function($id){
echo " get users by id " . $id;
})->filter(["id"=>"/[0-9]+/"]);
2- Custom use :, (*18)
- To customise the router name create a custom router class and extend the base Facade like this:
class MyRouter extends Aven\Facades\Facade{} // custom router class
MyRouter::get("/", function(){
return "Hello from my custom router";
});
- Or You can simply give an alias to the base
Facade
:
use Aven\Facades\Facade as MyRouter;
Aven Console (CLI) :
Aven CLI is a small tool aims to help you during development:, (*19)
, (*20)
configure Aven CLI, (*21)
make sure to :
- Include composer autoload file vendor/autoload.php
in your vendor/bin/aven
executable
- And include your routes file in the same file.
- You also can symlink ar copy vendor/bin/aven
to your project root .
or /usr/local/bin
to make it globally executable., (*22)
Your vendo/bin/aven
should have these two lines:, (*23)
require '../autoload.php';
require '../../routes/myRoutesFile.php'; // routes file
Available Commands :, (*24)
Assume we have moved vendor/bin/Aven
to ../../
our project location and we have added the routes and the autoload files., (*25)
1-routes : this command will list all defined routes giving you the ability to debug and see your routing table :, (*26)
, (*27)
2-Caching :, (*28)
Cashing is very important to speed up any web application therefore Aven
helps you to cache your routes and load from cache during production which increases your application speed.
By default Aven
doesn't allow Closure
caching which is the default behavoir of PHP However if you feel that you need to cache Closures Which is not recomended consider using SuperClusore package., (*29)
Setting up caching location :, (*30)
Aven::config([
"cache" => __DIR__ . "/cache"
]);
cache routes, (*31)
, (*32)
clear cache, (*33)
, (*34)
Custom Error Pages
You can use your custom error pages by defining your custom set_exception_handler
and translate error codes $exception->getCode()
to views., (*35)
-
Aven
trhows a NotFoundException
with 404
error code when no route has been matched or found.
- and it throws a
RegExMisMatchException
with 500
error code if a regular expression filter has not been matched.
TODO
Optional parameters support:, (*36)
Aven::get('/page/{id?}');
Adding some useful methods like, (*37)
Aven::group();
Aven::namespace();
Aven::form();
:computer: Contributing
- Thank you for considering to contribute to Package. All the contribution guidelines are mentioned here.
:page_with_curl: ChangeLog
:beer: Support the development
- Share Package and lets get more stars and more contributors.
- If this project helped you reduce time to develop, you can give me a cup of coffee :) : Paypal. 💖
:clipboard: License
-
Package is an open-source software licensed under the MIT license.