/** * Show the selected page * * @param $pagePath (String) * @return Response */ public function showPage(Request $request, $path = '') { // Redirect trailing slash $uri = $request->getRequestUri(); // if (preg_match('/(.+)\/$/', $request->getRequestUri()) !== 0) if (strlen($uri) > 1 && substr($uri, -1) === '/') { return redirect(rtrim($request->path(), '/'), 301); } // Parse the request path $pathInfo = LocaleService::parsePath($path); $pagePath = $pathInfo->pagePath; // Set the active language LocaleService::setLang($pathInfo->lang); // 'index' is a reserved name if ($pagePath === '/index') { error_log("index is a reserved name."); abort(404); } // Get the views' directory $viewsPath = base_path() . '/resources/views/'; if (!is_dir($viewsPath)) { error_log("The directory /resources/views/ doesn't exists."); throw new PathNotFoundException(); } // Compute the view name to be showed $showView = ''; if ($pagePath === '/') { $showView = 'index'; } else { $showView = ltrim(str_replace('/', '.', $pagePath), '.'); } // If the view doesn't exists return a 404 Not Found if (!view()->exists($showView)) { abort(404, "View not found: {$showView}"); } // Initialize the LocaleLinkService used inside views to set links LocaleLinkService::setLang($pathInfo->lang); LocaleLinkService::setLangDir($pathInfo->langDir); LocaleLinkService::setPagePath($pathInfo->pagePath); // Return the requested view return view($showView, ['benjamin' => 'benjamin::html', 'activeLang' => LocaleService::getActiveLang()]); }
/** * Given an url, returns the actual link, taking in accounts the current * language subdirectory and the current path. * If the url is an absolute path (i.e. starting with '/') will be prepended * the current language sub-directory. * If the url is a relative path, will translated to an absolute path where * the root is the language subdirectory. * If the url is not a path (absolute urls, fragments, etc) is left as it is. * * The current language subdirectory and the current path are taken from the * LocaleLinkService. * * If the multi-language support is disabled this method will do nothing and * returns the path as given. * * @param $url (String) * @return (String) */ function trlink($url) { // Check if the multi-language support is enabled if (!LocaleService::isMultilangEnabled()) { return $url; } // Parse the url $linkParts = parse_url($url); if ($linkParts === false) { return $url; } // It is an absolute url or there isn't no any path if (isset($linkParts['host']) || !isset($linkParts['path'])) { return $url; } // The link's path $linkPath = $linkParts['path']; // Get the current lang dir and the current page path $langDir = LocaleLinkService::getLangDir(); $currentPath = LocaleLinkService::getPagePath(); // Join the current path with the link path $resultUrl = ''; if (substr($linkPath, 0, 1) === '/') { // An absolute path => Replace the current one $resultUrl = $linkPath; } else { // Relative paths => Merge with the current one // Note: the $currentPath is always absolute (start with '/') and it never // contains a trailing slash ('/') if ($currentPath === '/') { $resultUrl = '/' . $linkPath; } else { $resultUrl = dirname($currentPath) . '/' . $linkPath; } } // Using http_build_url // Available here without PECL: // // Join the current path with the link path // $resParts = []; // $linkParts['path'] = $currentPath; // http_build_url( // $linkParts, // array('path' => $linkPath), // HTTP_URL_JOIN_PATH, // $resParts // ); // // Add the language dir // if (!empty($langDir)) { // $resParts['path'] = "/{$langDir}" . $resParts['path']; // } // return http_build_url($resParts); // Add the language dir if (!empty($langDir)) { $resultUrl = "/{$langDir}" . $resultUrl; } // Build the final url, preserving query string and fragment if (isset($linkParts['query'])) { $resultUrl .= '?' . $linkParts['query']; } if (isset($linkParts['fragment'])) { $resultUrl .= '#' . $linkParts['fragment']; } return $resultUrl; }
/** * Return a JSON object containing the content of all the pages. * * The returned object will have this structure: * * { * langDir: 'en', * pages: [{ * path: '/', * title: 'Home', * body: '<div> ... </div>' * bodyClass: 'some-class' * }, { * path: '/...', * title: '...', * body: '...' * bodyClass: '...' * }, * ... * ] * } * * @return JSON */ public function getAll(Request $request) { // Get the views' directory $viewsPath = base_path() . '/resources/views/'; if (!is_dir($viewsPath)) { error_log("The directory /resources/views/ doesn't exists."); throw new PathNotFoundException(); } // Get the lang' directory $langPath = base_path() . '/resources/lang/'; if (!is_dir($langPath)) { error_log("The directory /resources/lang/ doesn't exists."); throw new PathNotFoundException(); } // Get the language directory from the current url $path = $request->input('path'); $pathInfo = LocaleService::parsePath($path); // Set the active language LocaleService::setLang($pathInfo->lang); // Init the response object $resp = new \stdClass(); $resp->langDir = $pathInfo->langDir; $resp->pages = null; // Get last modification time on views or lang's messages $pagesLastMod = max(self::getLastMod($viewsPath), self::getLastMod($langPath)); // Init current cache keys (language-dependent) $pagesKey = "pages.{$pathInfo->lang}"; $pagesTimestampKey = "pages.{$pathInfo->lang}.timestamp"; // Check if the cache is enabled $cacheEnabled = false; if (env('CACHE_DRIVER') !== null) { $cacheEnabled = true; } // If the cache is valid return the cached value if ($cacheEnabled) { $pagesLastCache = Cache::get($pagesTimestampKey); if ($pagesLastCache !== null && $pagesLastCache >= $pagesLastMod) { $resp->pages = Cache::get($pagesKey); return response()->json($resp); } } // Get the list of views $viewsList = self::getViewsList($viewsPath); // Initialize the LocaleLinkService used inside views to set links LocaleLinkService::setLang($pathInfo->lang); LocaleLinkService::setLangDir($pathInfo->langDir); // Fill the $pages array, containing all the pages' content $pages = []; foreach ($viewsList as $viewName) { $page = new \stdClass(); // Page path $page->path = $viewName === 'index' ? '' : $viewName; $page->path = '/' . str_replace('.', '/', $page->path); // Set the page's path inside the LocaleLinkService LocaleLinkService::setPagePath($page->path); // Page title $page->title = view($viewName, ['benjamin' => 'benjamin::title', 'activeLang' => LocaleService::getActiveLang()])->render(); // Page body $page->body = view($viewName, ['benjamin' => 'benjamin::body', 'activeLang' => LocaleService::getActiveLang()])->render(); // bodyClass $page->bodyClass = view($viewName, ['benjamin' => 'benjamin::bodyClass', 'activeLang' => LocaleService::getActiveLang()])->render(); $pages[] = $page; } // Cache the value if ($cacheEnabled) { Cache::forever($pagesKey, $pages); Cache::forever($pagesTimestampKey, time()); } // Response $resp->pages = $pages; return response()->json($resp); }