Wallogit.com
2017 © Pedro Peláez
Simple router for light projects PHP
Very basic but functional router. It will be useful for simple projects, will help to implement the routing system quickly, as it is very easy and fast, you can use it both separately and in conjunction with other classes or patterns., (*1)
composer require bulveyz/router-php
use BulveyzRouter\Route;
use BulveyzRouter\Router;
Route::get('/home', function() {
echo "Home";
});
Route::any('/user/{id}', function($param) {
echo "User" . $param->id;
});
Route::post('/create/post', 'PostController@store');
Router::routeVoid();
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L,QSA]
Connect the necessary classes for the router BulveyzRouter\Route and BulveyzRouter\Router can be connected separately (if you use singleton), but BulveyzRouter\Route must be called and routes must be defined before BulveyzRouter\Router::routeVoid(); And don't forget to call BulveyzRouter\Router::routeVoid(); before defined routes., (*2)
use BulveyzRouter\Router; // Defained routes require_once '../routes.php'; // Run router Router::routeVoid();
use BulveyzRouter\Route;
Route::get('/home', function() {
echo "Home";
});
The parameters for the route are specified as {pattern}. The route should not have the same parameters, they should have different names., (*3)
Route::get('/user/{id}/{second_id}', function($params) {
echo $params->id . $params->second_id;
});
Route::get('/user/{id}/{second_id}', 'HomeController@index');
public function index($params)
{
echo $params->id . $params->second_id;
}
This version supports only 3 methods:, (*4)
You can specify the name of the router and return it anywhere., (*5)
Route::get('/home', 'HomeController@index')->name('home.index');
echo \route('home.index'); // return '/home'
Of course, you can change the namespace for controllers, by default it is App\ ., (*6)
Route::setNamespace('Your namespace');
"Route::setNamespace('Classes');"
var_dump(Route::routeList());
// Return namespace
echo Route::getNamespace()
// Return path for route
echo \route('routeName');
Route::get('/user', function (){
// Return method this route
echo Router::$method;
// Return path this route
echo Router::$route;
// Return params this route (Array)
var_dump(Router::$params);
// Return handler this route
var_dump(Router::$callback);
});