Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lotfio/aven
:tractor: Robust PHP router :tractor:
https://github.com/lotfio/aven
aven php-flexible-router php-router php-routing router
Last synced: 21 days ago
JSON representation
:tractor: Robust PHP router :tractor:
- Host: GitHub
- URL: https://github.com/lotfio/aven
- Owner: lotfio
- License: mit
- Created: 2018-04-26T16:55:31.000Z (over 6 years ago)
- Default Branch: develop
- Last Pushed: 2021-01-20T18:35:57.000Z (almost 4 years ago)
- Last Synced: 2024-07-05T14:05:40.438Z (4 months ago)
- Topics: aven, php-flexible-router, php-router, php-routing, router
- Language: PHP
- Homepage:
- Size: 130 KB
- Stars: 20
- Watchers: 6
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
:tractor: Robust PHP router :tractor:
### :fire: Introduction :
Aven (ayven) is a robust and flexible PHP router For PHP7 and newer versions.### :pushpin: Requirements :
- PHP 7.2 or newer versions
- PHPUnit >= 8 (for testing purpose)### :rocket: Installation & Use :
```php
composer require lotfio/aven
```### :pencil2: Usage :
**1- Quick use :**```php
get('/', function(){ // with a callback
return "welcome from aven";
});$router->get('/', "UsersController@method"); // controller method
$router->get('/', "UsersController::method"); // controller static method$router->init(); // initialize router
```
**2- Available routes :**
* `GET`, `POST`, `ANY`, `PUT`, `DELETE`, `HEAD`
```php
get('/', function(){ return " this is get method"; });
$router->post('/', function(){ return " this is post method"; });
$router->any('/', function(){ return " this is any method allows all"; });$router->put('/', function(){ return " this is put method. you should send $_POST['_method'] = 'put'"; });
$router->delete('/', function(){ return " this is delete method. you should send $_POST['_method'] = 'delete'"; });
$router->head('/', function(){ return " this is head method. you should send $_POST['_method'] = 'head'"; });
```**3- named routes :**
```php
get('/', function(){ return "this is get named route (default)";})->name('default');
```**4- redirects :**
* `$router->redirect(string $routeName, array $params = [], int $httpcode = 301)`
```php
get('/', function() use($router){ // accessing this route will redirect you to route2 means /hola$router->redirect('route2'); // if parametrised route you can pass array of parameters
})->name('default');
// route 2
$router->get('/hola', function(){ return " welcome to hola from default route";})->name('route2');
```**5- route parameters :**
* you can use both parenthesis or curly braces for parameters
* predefind parameters:
- `:int`, `:integer`, `:num`, `:numeric`, `:number` = **\d+**
- `:str` = **\w+**
- `:alpha` = **[A-z]+**
```php
get('/test/(:int)', function(){}); // evaluates to /test/\d+
$router->get('/test/(:str)', function(){}); // evaluates to /test/\w+// optional parameters (if optional parameter uri should end with /)
$router->get('/test/(:id*)', function(){}); // optional id /test/ or /test/1
$router->get('/test/(:id?)', function(){}); // zero or one id /test/ or /test/0-9```
**6- custom route parameters :**
* `->regex(array $params)`
```php
get('/test/(:str)', function(){})->regex(array(":str"=> '[my-reg-ex]'));// custom param
$router->get('/test/(:hola)', function(){})->regex(array(":hola"=> '[my-reg-ex]'));```
**7- route groups :**
* `$router->group(string $uri,callable $callback, ?string $groupName)`
- you can have as many nested groups as you want
```php
group('/mygroup', function($router){ // groups adds prefixes to routes$router->get('/test', function(){ return "from /mygroup/test" }); // evaluates to /mygroup/test
});
// multiple groups
$router->group('/group1', function($router){$router->group('/group2', function($router){
$router->get('/test', function(){ return "from /group1/group2/test" }); // evaluates to /group1/group2/test
});
});```
**8- route namespaces :**
* `$router->namespace(string $uri,callable $callback)`
- you can have as many nested namespaces as you want
```php
namespace('My\\Namespace', function($router){ // you can also use dots (My.Namespace) instead of \\
$router->get('/test', "TestController@test"}); // evaluates to My\\Namespace\\TestController@test
});// multiple groups
$router->namespace('ns1', function($router){$router->namespace('ns2', function($router){
$router->get('/test', "TestController@test"); // evaluates to ns1\\ns2\\TestController@test
});
});```
**9- additionl routes :**
* `$router->form(string $uri, $callback|$class, ?array $override, ?string $routeName)`
```php
form('/login', function(){ }); // works both with GET and POST// form route with class
$router->form('/login', Login::class); // by default class should have showForm & submitForm// override default form methods
$router->form('/login', Login::class, ['get','post']);// named form method
$router->form('/login', Login::class, ['get','post'], 'login.form');```
**10- additionl routes :**
* `$router->crud(string $uri, string $class, ?array $only, ?string $routeName)`
```php
POST user/create
// read => GET with optional pareter user/read/
// update => PUT with optional pareter user/update/
// delete => DELETE with optional pareter user/delete/
$router->crud('/user', User::class);// disable some methods
$router->crud('/user', User::class, ['c']); // only create
$router->crud('/user', User::class, ['create']); // only create
$router->crud('/user', User::class, ['c', 'u']); // create & update
$router->crud('/user', User::class, ['create', 'update']); // create & update// named crud
$router->crud('/user', User::class, NULL, 'myCrud'); // name can be used for redirections```
### :computer: Contributing
- Thank you for considering to contribute to ***Package***. All the contribution guidelines are mentioned [here](CONTRIBUTING.md).
### :page_with_curl: ChangeLog
- Here you can find the [ChangeLog](CHANGELOG.md).
### :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](https://www.paypal.me/lotfio)**. 💖### :clipboard: License
- ***Package*** is an open-source software licensed under the [MIT license](LICENSE).