PHP code example of underpin / route-loader

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

    

underpin / route-loader example snippets


plugin_name()->routes()->add( 'dashboard-home', [
	'name'        => 'Dashboard',
	'description' => "Route for dashboard screen",
    'id_callback' => function () { // Callback function to set the route ID. This should be unique.
		$pieces               = [ 'account' ];
		$account_screen       = get_query_var( 'account_screen', false );
		$account_child_screen = get_query_var( 'account_child_screen', false );

		if ( $account_screen ) {
			$pieces[] = $account_screen;
		}

		if ( $account_child_screen ) {
			$pieces[] = $account_child_screen;
		}

		return implode( '_', $pieces );
	},
	'priority'    => 'top', // Optional. sets the priority of add_rewrite_rule. Defaults to bottom. Can be "bottom" or "top"
	'route'       => '^account/?([A-Za-z0-9-]+)?/?([A-Za-z0-9-]+)?/?$', // Regex for route. See https://developer.wordpress.org/reference/functions/add_rewrite_rule/
	'query_vars'  => [ 'account_screen' => '$matches[1]', 'account_child_screen' => '$matches[2]' ], // Query vars for route. See https://developer.wordpress.org/reference/functions/add_rewrite_rule/
] );

plugin_name()->routes()->add('key','Namespace\To\Class');

plugin_name()->routes()->add( 'dashboard-home', [
	'name'        => 'Dashboard',
	'description' => "Route for dashboard screen",
	'route'       => '^account/?([A-Za-z0-9-]+)?/?([A-Za-z0-9-]+)?/?$', // Regex for route. See https://developer.wordpress.org/reference/functions/add_rewrite_rule/
	'query_vars'  => [ 'account_screen' => '$matches[1]', 'account_child_screen' => '$matches[2]' ], // Query vars for route. See https://developer.wordpress.org/reference/functions/add_rewrite_rule/
    'id_callback' => function () { // Callback function to set the route ID. This should be unique.
		$pieces               = [ 'account' ];
		$account_screen       = get_query_var( 'account_screen', false );
		$account_child_screen = get_query_var( 'account_child_screen', false );

		if ( $account_screen ) {
			$pieces[] = $account_screen;
		}

		if ( $account_child_screen ) {
			$pieces[] = $account_child_screen;
		}

		return implode( '_', $pieces );
	},
	'middlewares' => [
	  new \Underpin\Routes\Middlewares\Use_Template('/path/to/template/file.php')
    ]
] );

plugin_name()->routes()->add( 'dashboard-home', [
	'name'        => 'Dashboard',
	'description' => "Route for dashboard screen",
	'route'       => '^account/?([A-Za-z0-9-]+)?/?([A-Za-z0-9-]+)?/?$', // Regex for route. See https://developer.wordpress.org/reference/functions/add_rewrite_rule/
	'query_vars'  => [ 'account_screen' => '$matches[1]', 'account_child_screen' => '$matches[2]' ], // Query vars for route. See https://developer.wordpress.org/reference/functions/add_rewrite_rule/
    'id_callback' => function () { // Callback function to set the route ID. This should be unique.
		$pieces               = [ 'account' ];
		$account_screen       = get_query_var( 'account_screen', false );
		$account_child_screen = get_query_var( 'account_child_screen', false );

		if ( $account_screen ) {
			$pieces[] = $account_screen;
		}

		if ( $account_child_screen ) {
			$pieces[] = $account_child_screen;
		}

		return implode( '_', $pieces );
	},
	'middlewares' => [
	  new \Underpin\Routes\Middlewares\Prevent_Main_Query
    ]
] );

if( plugin_name()->routes()->is_current_route( 'route-id' ) ){
  // Do something specific to this route.
}

if( $route->is_current_route ){
  // Do something specific to this route.
}