Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added view() method to route collections #6568

Merged
merged 8 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions system/Router/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 fn (...$data) => Services::renderer()
->setData(['segments' => $data], 'raw')
->render($view, $options);

$this->create('get', $from, $to, $options);

return $this;
}

/**
* Limits the routes to a specified ENVIRONMENT or they won't run.
*/
Expand Down
21 changes: 21 additions & 0 deletions tests/system/Router/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,27 @@ 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);

// 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()
{
// ENVIRONMENT should be 'testing'
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <localization-in-routes>`
- Now you can specify Composer packages to auto-discover manually. See :ref:`Code Modules <modules-specify-composer-packages>`.
- Added new ``$routes->view()`` method to return a the view directly. See :ref:`View Routes <view-routes>`.

Changes
*******
Expand Down
16 changes: 16 additions & 0 deletions user_guide_src/source/incoming/routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,22 @@ a simple view:

.. literalinclude:: routing/020.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 is always treated as GET request.
This method accepts the name of the view to load as the second parameter.

.. 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.

.. literalinclude:: routing/066.php

Mapping Multiple Routes
=======================

Expand Down
4 changes: 4 additions & 0 deletions user_guide_src/source/incoming/routing/065.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

// Displays the view in /app/Views/pages/about.php
$routes->view('about', 'pages/about');
7 changes: 7 additions & 0 deletions user_guide_src/source/incoming/routing/066.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Displays the view in /app/Views/map.php
$routes->view('map/(:segment)/(:segment)', 'map');

// Within the view, you can access the segments with
// $segments[0] and $segments[1] respectively.