Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/erickfirmo/php.router
:elephant: Simple object-oriented PHP router
https://github.com/erickfirmo/php.router
oop php php-router poo route route-php router routes routing
Last synced: about 5 hours ago
JSON representation
:elephant: Simple object-oriented PHP router
- Host: GitHub
- URL: https://github.com/erickfirmo/php.router
- Owner: erickfirmo
- License: mit
- Created: 2019-09-24T03:52:51.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-05-04T17:44:00.000Z (over 2 years ago)
- Last Synced: 2024-10-08T12:37:08.631Z (about 1 month ago)
- Topics: oop, php, php-router, poo, route, route-php, router, routes, routing
- Language: PHP
- Homepage:
- Size: 70.3 KB
- Stars: 9
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
PHP Router Package
Simple object-oriented PHP router. Developed by Érick Firmo (BR) https://erickfirmo.dev
## :electric_plug: Prerequisites
- PHP >= 7
- URL rewrite## Install
To install with composer:```sh
composer require erickfirmo/router
```## Usage
```php
run();```
### Defining routes
Routes file example:
```php
get('/examples', ExampleController::class, 'index', 'examples.index');
$router->get('/examples/{id}', ExampleController::class, 'show', 'examples.show');
$router->post('/examples/store', ExampleController::class, 'store', 'examples.store');
$router->put('/examples/update/{id}', ExampleController::class, 'update', 'examples.update');
$router->patch('/examples/update/{id}', ExampleController::class, 'update', 'examples.update');
$router->delete('/examples/destroy/{id}', ExampleController::class, 'delete', 'examples.destroy');```
### Setting request HTTP verb in forms
To set the HTTP verb in your form action, create a input named `_method` with the verb value. The accepted values are `PUT`, `PATCH`, `DELETE`. Example:```
```This setting is not necessary to use routes GET or POST.
### Optional Settings
#### Namespace
If all of your manipulation classes are in the same namespace, you can set the default namespace to use in the router instance with `setNamespace(string $namespace)`:```php
setNamespace('App\Controllers\\');```
#### Error Page
By default, will be return a message error for routes not defined. You can set a custom page for this error, using `notFoundView(string $view)` method after instantiate the router:
```php
notFoundView(__DIR__.'/../views/errors/404.php');```
#### Passing Request Values
We can pass a request to our router using the `setRequest(string $name, Object|array $request)` method. This value will be used as the first argument of the called method. Example of using the request parameter:
```php
setRequest('request', $request);```
Getting the request parameter in example of controller:
```php
name;
echo $id;
}}
```
## :copyright: License
MIT License.
See [LICENSE](LICENSE) for details.
Connect me in LinkedIn | Click here to go to my CV