Simple object-oriented PHP router. Developed by Érick Firmo (BR) https://erickfirmo.dev
- PHP >= 7
- URL rewrite
To install with composer:
composer require erickfirmo/router
<?php
// Requires composer autoloader
require_once __DIR__ . '/vendor/autoload.php';
// Creating the router instance
$router = new \ErickFirmo\Router;
// Defining optional settings
// Load routes file
require_once __DIR__ . '/routes/web.php';
// Run the router
$router->run();
Routes file example:
<?php
$router->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');
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:
<input type="hidden" name="_method" value="PUT">
This setting is not necessary to use routes GET or POST.
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
$router->setNamespace('App\Controllers\\');
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
// Defining custom error page 404
$router->notFoundView(__DIR__.'/../views/errors/404.php');
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
// Creating array with request data
$request = $_SERVER['REQUEST_METHOD'] == 'POST' ? $_POST : $_GET;
// Passing request data
$router->setRequest('request', $request);
Getting the request parameter in example of controller:
<?php
class ExampleController
{
public function myMethod(array $request, int $id)
{
echo $request['name'];
echo $id;
}
}
We can also pass an object as a parameter:
<?php
class ExampleController
{
public function myMethod(Object $request, int $id)
{
echo $request->name;
echo $id;
}
}
MIT License.
See LICENSE for details.