PHP code example of eylmz / router

1. Go to this page and download the library: Download eylmz/router library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

eylmz / router example snippets




// z/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"]);

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

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

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

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

Router::get("url\{id?}",function($myID=0){
  echo $myID;
});
 
// Controller -> First Parameter
// Method -> Second Parameter
Router::get("admin\{controller}\{method}","{?}@{?}");

// or

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


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]+']);

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

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

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

$url = Router::route('profile', ['id' => 1]);
 
Router::prefix('admin')->group(function () {
    Router::get('users', function () {
        // new url -> /admin/users
    });
});

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

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

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

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