Skip to content
/ Router Public

Router - Fast, flexible routing for PHP, enabling you to quickly and easily build RESTful web applications.

License

Notifications You must be signed in to change notification settings

eylmz/Router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Router

Router - Fast, flexible routing for PHP, enabling you to quickly and easily build RESTful web applications.
Version : 1.0.0


Installation

You can download it and using it without any changes.

OR use Composer.

It's recommended that you use Composer to install Route.

$ composer require eylmz/router

Simple Usage

.htaccess

Options -Indexes
Options -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

index.php

<?php
require 'vendor/autoload.php';
// or
// require 'src/eylmz/Router.php';

use eylmz/Router/Router;

Router::setControllerNamespace("App\\Controllers\\");
Router::setMiddlewareNamespace("App\\Middlewares\\");

// Routers
Router::any("/url","Controller@Method");
// or
Router::any("/url2",function() {

});
// #Routers

Router::routeNow(@$_GET["url"]);

Available Router Methods

Router::get($url, $callback);
Router::post($url, $callback);
Router::put($url, $callback);
Router::patch($url, $callback);
Router::delete($url, $callback);
Router::options($url, $callback);

Usage More Than One Routers

Router::match("GET|POST",$url,$callback);
//or
Router::match(["GET","POST"],$url,$callback);

Usage The Any Methods

Router::any($url,$callback);

Route Parameters

Required Parameters

Router::get("url\{id}",function($myID){
  echo "Hello " . $myID;
});

Optional Parameters

Router::get("url\{id?}",function($myID=0){
  echo $myID;
});

Controller and Method Parameters

// Controller -> First Parameter
// Method -> Second Parameter
Router::get("admin\{controller}\{method}","{?}@{?}");

// or

// Custom
Router::get("admin\{method}\{controller}","{controller}@{method}");

Regular Expression Constraints

Router::get('url/{id}', function ($myID) {
    
})->where('id', '[0-9]+');

Router::get('user/{id}/{name}', function ($myID, $name) {
    
})->where(['id' => '[0-9]+', 'name' => '[a-zA-Z]+']);

Named Routes

Router::get('user/profile', function () {
    //
})->name('profile');

Generating URLs To Named Routes

$url = Router::route("profile");

// Usage with parameters
Router::get('url/{id}/profile', function ($id) {
    
})->name('profile');

$url = Router::route('profile', ['id' => 1]);

Route Groups

Prefix URL

Router::prefix('admin')->group(function () {
    Router::get('users', function () {
        // new url -> /admin/users
    });
});

Middleware

Router::middleware("middleware")->group(function () {
    Router::get('/', function () {
        
    });

    Router::get('url/profile', function () {
        
    });
});

Usage More Than One Middlewares

Router::middleware(["middleware","middleware2"])->group(function () {
    Router::get('/', function () {
        
    });

    Router::get('url/profile', function () {
        
    });
});

License

The MIT License (MIT). Please see License File for more information.

About

Router - Fast, flexible routing for PHP, enabling you to quickly and easily build RESTful web applications.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages