PHP code example of jinexus-framework / jinexus-route

1. Go to this page and download the library: Download jinexus-framework/jinexus-route 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/ */

    

jinexus-framework / jinexus-route example snippets


use JiNexus\Route\Route\Route;
use JiNexus\Route\Route\Factory\RouteFactory;
use JiNexus\Route\Redirect\Redirect;

// Via the factory (builds and wires a Redirect for you)...
$route = RouteFactory::build();

// ...or explicitly.
$route = new Route(new Redirect());

$route->setRoutes([
    'home'  => ['route' => '/',         'controller' => HomeController::class],
    'about' => ['route' => '/about-us', 'controller' => AboutController::class],
    'user'  => ['route' => '/user',     'controller' => UserController::class],
]);

$route->getRoutes();   // the full table
$route->setRoutes();   // called with no argument, resets to []

// Match an explicit URI.
$route->getMatchRoute([], '/about-us');
// ['about' => ['route' => '/about-us', 'controller' => AboutController::class]]

// Omit the URI to match against the current request.
$route->getMatchRoute();

// No match returns an empty array.
$route->getMatchRoute([], '/nope');   // []

$route->getMatchRoute(['about' => ['route' => '/about-us']], '/about-us');
// ['about' => ['route' => '/about-us']]

$routes = [
    'home' => ['route' => '/'],
    'user' => ['route' => '/user'],
];

$route->getRouteUri('home', $routes);              // '/'
$route->getRouteUri('user/profile/42', $routes);   // '/user' — only the first segment is looked up
$route->getRouteUri('missing', $routes);           // throws RouteException

// Omit the table to resolve against the registered routes.
$route->setRoutes($routes);
$route->getRouteUri('home');                       // '/'

// SCRIPT_NAME = /app/public/index.php
// REQUEST_URI = /app/public/user/42?ref=nav
$route->getUri();   // '/user/42'

use JiNexus\Route\Redirect\Redirect;
use JiNexus\Route\Redirect\Factory\RedirectFactory;

$redirect = RedirectFactory::build();   // or: new Redirect()

$redirect->setRoutes([
    'home'  => ['route' => '/'],
    'about' => ['route' => '/about-us'],
]);

$redirect->toRoute('about');         // 302 Location: /about-us, then exits
$redirect->toRoute('about', true);   // 301 Location: /about-us, then exits

$route->redirect;         // the RedirectInterface
$route->getRedirect();    // same, through AbstractBase::__call()

$route->getRedirect();              // reads the public $redirect
$route->setRedirect($otherOne);     // returns $route, so it chains

$redirect->getRoutes();             // throws: $routes is protected
// RouteException: Not implemented: JiNexus\Route\Redirect\Redirect::routes