/** * 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; }
/** * Return the last url the user has been on if detectable * * @return string */ public static function last() { return r::referer(); }
/** * 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 if ($this->options['tinyurl.enabled']) { $routes['tinyurl'] = array('pattern' => $this->options['tinyurl.folder'] . '/(:any)/(:any?)', 'action' => function ($hash, $lang = null) use($site) { // make sure the language is set $site->visit('/', $lang); // find the page by it's tiny hash if ($page = $site->index()->findBy('hash', $hash)) { go($page->url($lang)); } else { return $site->errorPage(); } }); } // all other urls $routes['others'] = array('pattern' => '(:all)', 'method' => 'ALL', 'action' => function ($path = null) use($site) { // 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 = 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)) { // TODO: put asset pipe here // redirect to the real file url to make this snappy go($file->url()); } } // return the error page if there's no such page return $site->errorPage(); } return $page; }); return $routes; }
public function testReferer() { $this->assertNull(r::referer()); }