r::referer PHP Method

referer() static public method

Returns the HTTP_REFERER
static public referer ( string $default = null ) : string
$default string Define a default URL if no referer has been found
return string
    static function referer($default = null)
    {
        if (empty($default)) {
            $default = '/';
        }
        return server::get('http_referer', $default);
    }

Usage Example

示例#1
0
 /**
  * Registers all routes
  *
  * @param array $routes New routes
  * @return array
  */
 public function routes($routes = array())
 {
     // extend the existing routes
     if (!empty($routes) and is_array($routes)) {
         return $this->options['routes'] = array_merge($this->options['routes'], $routes);
     }
     $routes = $this->options['routes'];
     $kirby = $this;
     $site = $this->site();
     if ($site->multilang()) {
         foreach ($site->languages() as $lang) {
             $routes[] = array('pattern' => ltrim($lang->url . '/(:all?)', '/'), 'method' => 'ALL', 'lang' => $lang, 'action' => function ($path = null) use($kirby, $site) {
                 return $site->visit($path, $kirby->route->lang->code());
             });
         }
         // fallback for the homepage
         $routes[] = array('pattern' => '/', 'method' => 'ALL', 'action' => function () use($kirby, $site) {
             // check if the language detector is activated
             if ($kirby->option('language.detect')) {
                 if (s::get('language') and $language = $kirby->site()->sessionLanguage()) {
                     // $language is already set but the user wants to
                     // select the default language
                     $referer = r::referer();
                     if (!empty($referer) && str::startsWith($referer, $this->urls()->index())) {
                         $language = $kirby->site()->defaultLanguage();
                     }
                 } else {
                     // detect the user language
                     $language = $kirby->site()->detectedLanguage();
                 }
             } else {
                 // always use the default language if the detector is disabled
                 $language = $kirby->site()->defaultLanguage();
             }
             // redirect to the language homepage if necessary
             if ($language->url != '/' and $language->url != '') {
                 go($language->url());
             }
             // plain home pages
             return $site->visit('/', $language->code());
         });
     }
     // tinyurl handling
     $routes['tinyurl'] = $this->component('tinyurl')->route();
     // home redirect
     $routes['homeRedirect'] = array('pattern' => $this->options['home'], 'action' => function () {
         redirect::send(page('home')->url(), 307);
     });
     // plugin assets
     $routes['pluginAssets'] = array('pattern' => 'assets/plugins/(:any)/(:all)', 'method' => 'GET', 'action' => function ($plugin, $path) use($kirby) {
         $root = $kirby->roots()->plugins() . DS . $plugin . DS . 'assets' . DS . $path;
         $file = new Media($root);
         if ($file->exists()) {
             return new Response(f::read($root), f::extension($root));
         } else {
             return new Response('The file could not be found', f::extension($path), 404);
         }
     });
     // all other urls
     $routes['others'] = array('pattern' => '(:all)', 'method' => 'ALL', 'action' => function ($path = null) use($site, $kirby) {
         // visit the currently active page
         $page = $site->visit($path);
         // react on errors for invalid URLs
         if ($page->isErrorPage() and $page->uri() != $path) {
             // get the filename
             $filename = rawurldecode(basename($path));
             $pagepath = dirname($path);
             // check if there's a page for the parent path
             if ($page = $site->find($pagepath)) {
                 // check if there's a file for the last element of the path
                 if ($file = $page->file($filename)) {
                     go($file->url());
                 }
             }
             // return the error page if there's no such page
             return $site->errorPage();
         }
         return $page;
     });
     return $routes;
 }
All Usage Examples Of r::referer