devpontes/route

Simples componente de rotas para projetos pequenos baseados em MVC

v2.1.2 2024-04-19 18:45 UTC

This package is auto-updated.

Last update: 2025-05-01 00:13:31 UTC


README

Maintainer Source Code PHP from Packagist Latest Version Software License Build Quality Score Total Downloads

About Route componet

Route is a simple route component for small MVC-based projects

Route é um simples componente de rotas para projetos pequenos baseados em MVC.

Highlights

  • Rotas nomeadas. (named routes)
  • Verbos GET e POST. (GET and POST verbs)
  • Simples de configurar. (simple to set up)
  • Padrão controller/método/{paramentro}. (pattern controller/method/{parameter}).

Installation

Installation is available through Composer:

"devpontes/route": "2.*"

or run

composer require devpontes/route

Documentation

To use the route, it is necessary to redirect all application requests to the index.php file, which will be the Front Controller, where all application traffic will be handled, see the example
  1. Para usar o route é necessário redirecionar todas as requisições da aplicação para o arquivo index.php que será o Front Controller, onde todo o tráfego da aplicação será tratado, veja o exemplo:

Apache

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Configure an array with the application routes
  1. Configure um array com as rotas da aplicação.
<?php

// Define routes
$routes = [
    ['/','Home@index'],
    ['/about', 'About@index'],
    ['/contact', 'Contact@index'],
    ['/blog/{artigo}', 'Blog@index'],
];
At initialization, enter the route array. Then, use the namespace method passing the path that was configured in the composer.json autoload. Lastly, use rum to execute
  1. Na inicialização, informe o array de rotas. Em seguida, utilize o método namespace passando o caminho que foi configurado no autoload do composer.json. Por último, use o rum para executar.

Usage

$namespace = "App\Controller";
$route = new \DevPontes\Route\Route($routes);
$route->namespace($namespace);
$route->run();
To handle unconfigured routes, use the fail method, see
  • Para tratar rotas não configuradas use o método fail, veja:
// Redirect
if ($route->fail()) {
    header('Location: /not-found');
}

Credits

License

The MIT License (MIT).