From 3eca92a0682c949a6cfb53b75cf4e10c7c83d301 Mon Sep 17 00:00:00 2001 From: Lonnie Ezell Date: Fri, 9 Sep 2022 23:22:08 -0500 Subject: [PATCH 1/8] Added view() method to route collections. --- system/Router/RouteCollection.php | 15 +++++++++++++++ tests/system/Router/RouteCollectionTest.php | 12 ++++++++++++ user_guide_src/source/changelogs/v4.3.0.rst | 1 + user_guide_src/source/incoming/routing.rst | 19 +++++++++++++++++-- .../source/incoming/routing/065.php | 4 ++++ .../source/incoming/routing/066.php | 7 +++++++ 6 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 user_guide_src/source/incoming/routing/065.php create mode 100644 user_guide_src/source/incoming/routing/066.php diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index 8567c17f03b3..cce069cd96f0 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -992,6 +992,21 @@ public function cli(string $from, $to, ?array $options = null): RouteCollectionI return $this; } + /** + * Specifies a route that will only display a view. + * Only works for GET requests. + */ + public function view(string $from, string $view, ?array $options = null): RouteCollectionInterface + { + $to = static function (...$data) use ($view) { + return view($view, ['_page' => $data]); + }; + + $this->create('get', $from, $to, $options); + + return $this; + } + /** * Limits the routes to a specified ENVIRONMENT or they won't run. */ diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index 5f6486cb5aec..b86fabd1c81e 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -12,9 +12,11 @@ namespace CodeIgniter\Router; use App\Controllers\Home; +use CodeIgniter\Config\Factory; use CodeIgniter\Config\Services; use CodeIgniter\Exceptions\PageNotFoundException; use CodeIgniter\Test\CIUnitTestCase; +use CodeIgniter\View\View; use Config\Modules; use Tests\Support\Controllers\Hello; @@ -819,6 +821,16 @@ public function testCLI() $this->assertSame($expected, $routes->getRoutes('cli')); } + public function testView() + { + $routes = $this->getCollector(); + + $routes->view('here', 'hello'); + + $route = $routes->getRoutes('get')['here']; + $this->assertIsCallable($route); + } + public function testEnvironmentRestricts() { // ENVIRONMENT should be 'testing' diff --git a/user_guide_src/source/changelogs/v4.3.0.rst b/user_guide_src/source/changelogs/v4.3.0.rst index 85bf1f421083..e1101bddac97 100644 --- a/user_guide_src/source/changelogs/v4.3.0.rst +++ b/user_guide_src/source/changelogs/v4.3.0.rst @@ -136,6 +136,7 @@ Others - Added ``$routes->useSupportedLocalesOnly(true)`` so that the Router returns 404 Not Found if the locale in the URL is not supported in ``Config\App::$supportedLocales``. See :ref:`Localization ` - Now you can specify Composer packages to auto-discover manually. See :ref:`Code Modules `. +- Added new ``$routes->view()`` method to echo the view directly. See :ref:`View Routes `. Changes ******* diff --git a/user_guide_src/source/incoming/routing.rst b/user_guide_src/source/incoming/routing.rst index cbe7359b22bc..f9fdb0233fc7 100644 --- a/user_guide_src/source/incoming/routing.rst +++ b/user_guide_src/source/incoming/routing.rst @@ -206,9 +206,24 @@ Closures You can use an anonymous function, or Closure, as the destination that a route maps to. This function will be executed when the user visits that URI. This is handy for quickly executing small tasks, or even just showing -a simple view: +a simple view. -.. literalinclude:: routing/020.php +.. literalinclude:: routing/065.php + +If you use placeholders within your route, you can access them within the view in a special variable, ``$_page``. +They are available as an array, indexed in the order they appear in the route. + +.. literalinclude:: routing/066.php + +.. _view_routes: + +Views +===== + +If you just want to render a view out that has no logic associated with it, you can use the ``view()`` method. +This method accepts the name of the view to load as the ``$to`` parameter. + +.. literalinclude:: routing/021.php Mapping Multiple Routes ======================= diff --git a/user_guide_src/source/incoming/routing/065.php b/user_guide_src/source/incoming/routing/065.php new file mode 100644 index 000000000000..ad3c10094075 --- /dev/null +++ b/user_guide_src/source/incoming/routing/065.php @@ -0,0 +1,4 @@ +view('about', 'pages/about'); diff --git a/user_guide_src/source/incoming/routing/066.php b/user_guide_src/source/incoming/routing/066.php new file mode 100644 index 000000000000..e213a4d96224 --- /dev/null +++ b/user_guide_src/source/incoming/routing/066.php @@ -0,0 +1,7 @@ +view('map/(:segment)/(:segment)', 'map'); + +// Within the view, you can access the segments with +// $_page[0] and $_page[1] respectively. From a4f62dd7e3cfd8966336ff0cfdf5b5d327733a7b Mon Sep 17 00:00:00 2001 From: Lonnie Ezell Date: Fri, 9 Sep 2022 23:28:24 -0500 Subject: [PATCH 2/8] Style fix --- system/Router/RouteCollection.php | 4 +--- tests/system/Router/RouteCollectionTest.php | 2 -- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index cce069cd96f0..92321ba12db1 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -998,9 +998,7 @@ public function cli(string $from, $to, ?array $options = null): RouteCollectionI */ public function view(string $from, string $view, ?array $options = null): RouteCollectionInterface { - $to = static function (...$data) use ($view) { - return view($view, ['_page' => $data]); - }; + $to = static fn (...$data) => view($view, ['_page' => $data]); $this->create('get', $from, $to, $options); diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index b86fabd1c81e..4ba6c164b034 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -12,11 +12,9 @@ namespace CodeIgniter\Router; use App\Controllers\Home; -use CodeIgniter\Config\Factory; use CodeIgniter\Config\Services; use CodeIgniter\Exceptions\PageNotFoundException; use CodeIgniter\Test\CIUnitTestCase; -use CodeIgniter\View\View; use Config\Modules; use Tests\Support\Controllers\Hello; From 50fdf16f65a2f376f795b5e0c0325b60c38ee509 Mon Sep 17 00:00:00 2001 From: Lonnie Ezell Date: Fri, 9 Sep 2022 23:28:58 -0500 Subject: [PATCH 3/8] GET note --- user_guide_src/source/incoming/routing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/incoming/routing.rst b/user_guide_src/source/incoming/routing.rst index f9fdb0233fc7..208f7dcf6e6e 100644 --- a/user_guide_src/source/incoming/routing.rst +++ b/user_guide_src/source/incoming/routing.rst @@ -206,7 +206,7 @@ Closures You can use an anonymous function, or Closure, as the destination that a route maps to. This function will be executed when the user visits that URI. This is handy for quickly executing small tasks, or even just showing -a simple view. +a simple view. These are always treated as GET requests. .. literalinclude:: routing/065.php From a38c5cd7d132d1ce6b4946061ccb48be3417fe3a Mon Sep 17 00:00:00 2001 From: Lonnie Ezell Date: Sun, 11 Sep 2022 00:02:33 -0500 Subject: [PATCH 4/8] Addressing review comments --- system/Router/RouteCollection.php | 2 +- user_guide_src/source/incoming/routing.rst | 12 ++++++------ user_guide_src/source/incoming/routing/066.php | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index 92321ba12db1..ebd1592ec6ea 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -998,7 +998,7 @@ public function cli(string $from, $to, ?array $options = null): RouteCollectionI */ public function view(string $from, string $view, ?array $options = null): RouteCollectionInterface { - $to = static fn (...$data) => view($view, ['_page' => $data]); + $to = static fn (...$data) => view($view, ['page' => $data]); $this->create('get', $from, $to, $options); diff --git a/user_guide_src/source/incoming/routing.rst b/user_guide_src/source/incoming/routing.rst index 208f7dcf6e6e..f62401aa3621 100644 --- a/user_guide_src/source/incoming/routing.rst +++ b/user_guide_src/source/incoming/routing.rst @@ -210,12 +210,7 @@ a simple view. These are always treated as GET requests. .. literalinclude:: routing/065.php -If you use placeholders within your route, you can access them within the view in a special variable, ``$_page``. -They are available as an array, indexed in the order they appear in the route. - -.. literalinclude:: routing/066.php - -.. _view_routes: +.. _view-routes: Views ===== @@ -225,6 +220,11 @@ This method accepts the name of the view to load as the ``$to`` parameter. .. literalinclude:: routing/021.php +If you use placeholders within your route, you can access them within the view in a special variable, ``$page``. +They are available as an array, indexed in the order they appear in the route. + +.. literalinclude:: routing/066.php + Mapping Multiple Routes ======================= diff --git a/user_guide_src/source/incoming/routing/066.php b/user_guide_src/source/incoming/routing/066.php index e213a4d96224..eeeaa6c95064 100644 --- a/user_guide_src/source/incoming/routing/066.php +++ b/user_guide_src/source/incoming/routing/066.php @@ -4,4 +4,4 @@ $routes->view('map/(:segment)/(:segment)', 'map'); // Within the view, you can access the segments with -// $_page[0] and $_page[1] respectively. +// $page[0] and $page[1] respectively. From 39173e3037cdfe64f1931186f702223e42d2f791 Mon Sep 17 00:00:00 2001 From: Lonnie Ezell Date: Wed, 14 Sep 2022 22:59:06 -0500 Subject: [PATCH 5/8] addressing review comments. --- system/Router/RouteCollection.php | 4 +++- tests/system/Router/RouteCollectionTest.php | 11 +++++++++++ user_guide_src/source/changelogs/v4.3.0.rst | 2 +- user_guide_src/source/incoming/routing.rst | 2 +- user_guide_src/source/incoming/routing/066.php | 2 +- 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index ebd1592ec6ea..abf524292bb1 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -998,7 +998,9 @@ public function cli(string $from, $to, ?array $options = null): RouteCollectionI */ public function view(string $from, string $view, ?array $options = null): RouteCollectionInterface { - $to = static fn (...$data) => view($view, ['page' => $data]); + $to = static fn (...$data) => Services::renderer() + ->setData(['segments' => $data], 'raw') + ->render($view, $options); $this->create('get', $from, $to, $options); diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index 4ba6c164b034..cae486f063ac 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -827,6 +827,17 @@ public function testView() $route = $routes->getRoutes('get')['here']; $this->assertIsCallable($route); + + // Test that the route is not available in any other verb + $this->assertArrayNotHasKey('here', $routes->getRoutes('*')); + $this->assertArrayNotHasKey('here', $routes->getRoutes('options')); + $this->assertArrayNotHasKey('here', $routes->getRoutes('head')); + $this->assertArrayNotHasKey('here', $routes->getRoutes('post')); + $this->assertArrayNotHasKey('here', $routes->getRoutes('put')); + $this->assertArrayNotHasKey('here', $routes->getRoutes('delete')); + $this->assertArrayNotHasKey('here', $routes->getRoutes('trace')); + $this->assertArrayNotHasKey('here', $routes->getRoutes('connect')); + $this->assertArrayNotHasKey('here', $routes->getRoutes('cli')); } public function testEnvironmentRestricts() diff --git a/user_guide_src/source/changelogs/v4.3.0.rst b/user_guide_src/source/changelogs/v4.3.0.rst index e1101bddac97..fd00f93c25a9 100644 --- a/user_guide_src/source/changelogs/v4.3.0.rst +++ b/user_guide_src/source/changelogs/v4.3.0.rst @@ -136,7 +136,7 @@ Others - Added ``$routes->useSupportedLocalesOnly(true)`` so that the Router returns 404 Not Found if the locale in the URL is not supported in ``Config\App::$supportedLocales``. See :ref:`Localization ` - Now you can specify Composer packages to auto-discover manually. See :ref:`Code Modules `. -- Added new ``$routes->view()`` method to echo the view directly. See :ref:`View Routes `. +- Added new ``$routes->view()`` method to return a the view directly. See :ref:`View Routes `. Changes ******* diff --git a/user_guide_src/source/incoming/routing.rst b/user_guide_src/source/incoming/routing.rst index f62401aa3621..e8110546e869 100644 --- a/user_guide_src/source/incoming/routing.rst +++ b/user_guide_src/source/incoming/routing.rst @@ -220,7 +220,7 @@ This method accepts the name of the view to load as the ``$to`` parameter. .. literalinclude:: routing/021.php -If you use placeholders within your route, you can access them within the view in a special variable, ``$page``. +If you use placeholders within your route, you can access them within the view in a special variable, ``$segments``. They are available as an array, indexed in the order they appear in the route. .. literalinclude:: routing/066.php diff --git a/user_guide_src/source/incoming/routing/066.php b/user_guide_src/source/incoming/routing/066.php index eeeaa6c95064..10f867f186fc 100644 --- a/user_guide_src/source/incoming/routing/066.php +++ b/user_guide_src/source/incoming/routing/066.php @@ -4,4 +4,4 @@ $routes->view('map/(:segment)/(:segment)', 'map'); // Within the view, you can access the segments with -// $page[0] and $page[1] respectively. +// $segments[0] and $segments[1] respectively. From 42d6d916f2e9a3def6ae49abb294cc9663d97b77 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 23 Sep 2022 14:21:31 +0900 Subject: [PATCH 6/8] docs: fix accidental changes --- user_guide_src/source/incoming/routing.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/user_guide_src/source/incoming/routing.rst b/user_guide_src/source/incoming/routing.rst index e8110546e869..1800dcc21c80 100644 --- a/user_guide_src/source/incoming/routing.rst +++ b/user_guide_src/source/incoming/routing.rst @@ -206,9 +206,9 @@ Closures You can use an anonymous function, or Closure, as the destination that a route maps to. This function will be executed when the user visits that URI. This is handy for quickly executing small tasks, or even just showing -a simple view. These are always treated as GET requests. +a simple view. -.. literalinclude:: routing/065.php +.. literalinclude:: routing/020.php .. _view-routes: @@ -216,9 +216,10 @@ Views ===== If you just want to render a view out that has no logic associated with it, you can use the ``view()`` method. +This is always treated as GET request. This method accepts the name of the view to load as the ``$to`` parameter. -.. literalinclude:: routing/021.php +.. literalinclude:: routing/065.php If you use placeholders within your route, you can access them within the view in a special variable, ``$segments``. They are available as an array, indexed in the order they appear in the route. From 122129e33e6e30223e92e26adf6edcabc0516b11 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 23 Sep 2022 14:23:50 +0900 Subject: [PATCH 7/8] docs: fix parameter name There is no $to parameter in the view() method. --- user_guide_src/source/incoming/routing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/incoming/routing.rst b/user_guide_src/source/incoming/routing.rst index 1800dcc21c80..6aefd9a72efd 100644 --- a/user_guide_src/source/incoming/routing.rst +++ b/user_guide_src/source/incoming/routing.rst @@ -217,7 +217,7 @@ Views If you just want to render a view out that has no logic associated with it, you can use the ``view()`` method. This is always treated as GET request. -This method accepts the name of the view to load as the ``$to`` parameter. +This method accepts the name of the view to load as the second parameter. .. literalinclude:: routing/065.php From 99afa616a7fe0b10ca2b169b01ad58c4043a2b6c Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 23 Sep 2022 16:25:41 +0900 Subject: [PATCH 8/8] docs: replace "." with ":" --- user_guide_src/source/incoming/routing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/incoming/routing.rst b/user_guide_src/source/incoming/routing.rst index 6aefd9a72efd..71b182df328b 100644 --- a/user_guide_src/source/incoming/routing.rst +++ b/user_guide_src/source/incoming/routing.rst @@ -206,7 +206,7 @@ Closures You can use an anonymous function, or Closure, as the destination that a route maps to. This function will be executed when the user visits that URI. This is handy for quickly executing small tasks, or even just showing -a simple view. +a simple view: .. literalinclude:: routing/020.php