Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

Awesome Lists containing this project

README

        


PHP Router

PHP Router Package


Simple object-oriented PHP router. Developed by Érick Firmo (BR) https://erickfirmo.dev


GitHub language count
Total Downloads
Latest Stable Version

Made by Erick Firmo

License

## :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