Airship\Cabin\Hull\Blueprint\CustomPages::serveRedirect PHP Method

serveRedirect() public method

If a redirect exists at this path, serve it.
public serveRedirect ( string $uri ) : boolean
$uri string
return boolean
    public function serveRedirect(string $uri) : bool
    {
        $lookup = $this->db->row('SELECT * FROM airship_custom_redirect WHERE oldpath = ?', $uri);
        if (empty($lookup)) {
            return false;
        }
        if ($lookup['same_cabin']) {
            // Internal redirects only. Don't create open redirect vulnerabilities.
            \Airship\redirect(\Airship\LensFunctions\cabin_url($lookup['cabin']) . \trim($lookup['newpath'], '/'));
        }
        // Cross-cabin redirects can point to other domains.
        \Airship\redirect(\rtrim($lookup['newpath'], '/'));
        return true;
    }

Usage Example

Example #1
0
 /**
  * This interrupts requests if all else fails.
  * @param string[] ...$args
  * @return void
  * @throws CustomPageNotFoundException
  */
 public function routeNotFound(...$args)
 {
     if (!\is1DArray($args)) {
         throw new CustomPageNotFoundException(\__('Invalid arguments'));
     }
     $dirs = $args;
     $file = \array_pop($dirs);
     // First: Do we have a custom page at this endpoint?
     try {
         if ($this->serveCustomPage($file, $dirs)) {
             return;
         }
     } catch (CustomPageNotFoundException $ex) {
         $this->log('Custom page not found', LogLevel::INFO, ['cabin' => $this->cabin, 'dirs' => $dirs, 'exception' => \Airship\throwableToArray($ex), 'file' => $file]);
     }
     // Second: Is there a redirect at this endpoint?
     try {
         $path = \implode('/', $args);
         if ($this->pages->serveRedirect($path)) {
             return;
         }
     } catch (RedirectException $ex) {
         $this->log('Redirect missed', LogLevel::INFO);
     }
     \http_response_code(404);
     // Finally: Return a 4o4
     $this->lens('404');
     return;
 }