Jump to main content Jump to doc navigation

modX::sendForward

Forwards the request to another resource without changing the URL. If the ID provided does not exist, sends to a 404 Error page.

Syntax

void sendForward (integer $id, [string|array $options = null], [boolean $sendErrorPage = true])
  • $id (integer) The target is a Resource ID, not a URL — to pass a value to the target, set a placeholder with modX.setPlaceholder and read it there.
  • $options (string|array) A string is assumed to be an HTTP response code, e.g. HTTP/1.1 301 Moved Permanently. An array accepts the following keys:
    • response_code: response code
    • error_type: response code type, e.g. 404
    • error_header: "Header:" value
    • error_pagetitle: error pagetitle value
    • error_message: error message
    • merge: a way to merge the resource currently in $modx->resource with the target resource. The content, pub_date, unpub_date, richtext, _content and _processed values are excluded as well as the value of the forward_merge_excludes system setting.
  • $sendErrorPage (boolean) Whether we should skip the sendErrorPage if the resource does not exist.

Examples

Send the user to Resource ID 234 without changing the URL.

$modx->sendForward(234);

Send the user to a 404 error page. The page ID comes from the error_page system setting; without it, site_start is used.

$options = array(
   'response_code' => '404 Not Found',
   'error_type' => '404',
   'error_header' => '404 Not Found',
   'error_pagetitle' => 'Error 404: Page not found',
   'error_message' => '<h1>Page not found</h1><p>The page you requested was not found.</p>'
);
$modx->sendForward($modx->getOption('error_page', $options, $modx->getOption('site_start')), $options, false);

Show a replacement page while keeping the original pagetitle, introtext and other fields — pass an extra array with these keys:

$options = array(
	'merge' => 1, // field gluing mechanism enabled
	// original fields list that need to be excluded from the result
	'forward_merge_excludes' => 'id,template,type,published,class_key'
);
$modx->sendForward(15, $options);

Useful when you hide or protect a resource but keep its pagetitle and description for visitors and search crawlers.

See Also

Support the team building MODX with a monthly donation.

The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.

Backers

  • modmore
  • STERC
  • Digital Penguin
  • Jens Wittmann – Gestaltung & Entwicklung
  • CrewMark
  • Fabian Christen
  • Sepia River Studios
  • Dannevang Digital
  • Alex
  • A. Moreno
  • Chris Fickling
  • Stéphane Jäggi
  • Murray Wood
  • Anton Tarasov
  • JT Skaggs
  • deJaya
  • Lefthandmedia
  • eydolan
  • Following Sea
  • Guido Gallenkamp
  • YJ
  • Raffy
  • Snow Creative
  • Nick Clark
  • Guest
  • Helen
  • krisznet
  • Yanni
  • Richard

Budget

$194 per month—let's make that $500!

Learn more