# Router

This component implements callbacks and methods for managing URLs and redirects. In HivePress, each URL is registered as a route with an array of parameters. This makes URLs customizable and accessible via the **Router** methods.

To get the current route name, call the `get_current_route_name` method:

```php
$route = hivepress()->router->get_current_route_name();
```

You can also customize any URL route via the `hivepress/v1/routes` filter hook. Remember to refresh permalinks in **Settings > Permalinks** after making any URL changes.

### Quick example <a href="#bkmrk-quick-example" id="bkmrk-quick-example"></a>

The code example below redirects non-registered users from the listing page to the login page. After the user is logged in or registered, there's a redirect back to the initial listing page.

```php
add_action(
	'template_redirect',
	function() {
		if ( ! is_user_logged_in() && hivepress()->router->get_current_route_name() === 'listing_view_page' ) {
			wp_safe_redirect( hivepress()->router->get_return_url( 'user_login_page' ) );

			exit;
		}
	}
);
```

### Getting URLs <a href="#bkmrk-getting-urls" id="bkmrk-getting-urls"></a>

To get the current URL, call the `get_current_url` method:

```php
$url = hivepress()->router->get_current_url();
```

For getting a URL by the route name, use the `get_url` method:

```php
$url = hivepress()->router->get_url( 'my_custom_route' );
```

Some routes require extra URL parameters. The code example below gets a listing URL by ID:

```php
$url = hivepress()->router->get_url( 'listing_view_page', [ 'listing_id' => 123 ] );
```

To get a URL that redirects users back after some action (e.g. logging in), use the `get_return_url` method:

```php
$url = hivepress()->router->get_return_url( 'my_custom_route' );
```

If you need to get the redirect URL from the current URL query parameters, call the `get_redirect_url` method:

```php
$url = hivepress()->router->get_redirect_url();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hivepress.io/developer-docs/framework/components/router.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
