/** * 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()]); }
/** * 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); }