PagesController.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Static content controller.
  5. *
  6. * This file will render views from views/pages/
  7. *
  8. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  9. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. *
  11. * Licensed under The MIT License
  12. * For full copyright and license information, please see the LICENSE.txt
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  16. * @link https://cakephp.org CakePHP(tm) Project
  17. * @since 0.2.9
  18. * @license https://opensource.org/licenses/mit-license.php MIT License
  19. */
  20. namespace TestApp\Controller;
  21. use Cake\Core\Configure;
  22. use Cake\Http\Exception\NotFoundException;
  23. use Cake\Utility\Inflector;
  24. use Cake\View\Exception\MissingTemplateException;
  25. /**
  26. * Static content controller
  27. */
  28. class PagesController extends AppController
  29. {
  30. /**
  31. * Displays a view
  32. *
  33. * @param mixed What page to display
  34. * @return \Cake\Http\Response|null
  35. * @throws \Cake\Http\Exception\NotFoundException When the view file could not be found.
  36. * @throws \Cake\View\Exception\MissingTemplateException In debug mode.
  37. */
  38. public function display(...$path)
  39. {
  40. $count = count($path);
  41. if (!$count) {
  42. return $this->redirect('/');
  43. }
  44. $page = null;
  45. $subpage = null;
  46. $titleForLayout = null;
  47. if (!empty($path[0])) {
  48. $page = $path[0];
  49. }
  50. if (!empty($path[1])) {
  51. $subpage = $path[1];
  52. }
  53. if (!empty($path[$count - 1])) {
  54. $titleForLayout = Inflector::humanize($path[$count - 1]);
  55. }
  56. $this->set([
  57. 'page' => $page,
  58. 'subpage' => $subpage,
  59. 'title_for_layout' => $titleForLayout,
  60. ]);
  61. try {
  62. $this->render(implode('/', $path));
  63. } catch (MissingTemplateException $e) {
  64. if (Configure::read('debug')) {
  65. throw $e;
  66. }
  67. throw new NotFoundException();
  68. }
  69. }
  70. }