mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #8838 from creative-commoners/pulls/4/slash-means-root
Use '/' as an alternative designation for root in routing
This commit is contained in:
commit
c31de772ab
@ -216,6 +216,30 @@ Director:
|
|||||||
'feed': 'FeedController'
|
'feed': 'FeedController'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Root URL Handlers
|
||||||
|
|
||||||
|
In some cases, the Director rule covers the entire URL you intend to match, and you simply want the controller to respond to a 'root' request. This request will automatically direct to an `index()` method if it exists on the controller, but you can also set a custom method to use in `$url_handlers` with the `'/'` key:
|
||||||
|
|
||||||
|
```php
|
||||||
|
use SilverStripe\Control\Controller;
|
||||||
|
|
||||||
|
class BreadAPIController extends Controller
|
||||||
|
{
|
||||||
|
private static $allowed_actions = [
|
||||||
|
'getBreads',
|
||||||
|
'createBread',
|
||||||
|
];
|
||||||
|
|
||||||
|
private static $url_handlers = [
|
||||||
|
'GET /' => 'getBreads',
|
||||||
|
'POST /' => 'createBread',
|
||||||
|
];
|
||||||
|
```
|
||||||
|
|
||||||
|
<div class="alert" markdown="1">
|
||||||
|
In SilverStripe Framework versions prior to 4.6, an empty key (`''`) must be used in place of the `'/'` key. When specifying an HTTP method, the empty string must be separated from the method (e.g. `'GET '`). The empty key and slash key are also equivalent in Director rules.
|
||||||
|
</div>
|
||||||
|
|
||||||
## Related Lessons
|
## Related Lessons
|
||||||
* [Creating filtered views](https://www.silverstripe.org/learn/lessons/v4/creating-filtered-views-1)
|
* [Creating filtered views](https://www.silverstripe.org/learn/lessons/v4/creating-filtered-views-1)
|
||||||
* [Controller actions / DataObjects as pages](https://www.silverstripe.org/learn/lessons/v4/controller-actions-dataobjects-as-pages-1)
|
* [Controller actions / DataObjects as pages](https://www.silverstripe.org/learn/lessons/v4/controller-actions-dataobjects-as-pages-1)
|
||||||
|
@ -523,8 +523,8 @@ class HTTPRequest implements ArrayAccess
|
|||||||
$pattern = $matches[2];
|
$pattern = $matches[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Special case for the root URL controller
|
// Special case for the root URL controller (designated as an empty string, or a slash)
|
||||||
if (!$pattern) {
|
if (!$pattern || $pattern === '/') {
|
||||||
return ($this->dirParts == array()) ? array('Matched' => true) : false;
|
return ($this->dirParts == array()) ? array('Matched' => true) : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@ use SilverStripe\Control\Tests\ControllerTest\IndexSecuredController;
|
|||||||
use SilverStripe\Control\Tests\ControllerTest\SubController;
|
use SilverStripe\Control\Tests\ControllerTest\SubController;
|
||||||
use SilverStripe\Control\Tests\ControllerTest\TestController;
|
use SilverStripe\Control\Tests\ControllerTest\TestController;
|
||||||
use SilverStripe\Control\Tests\ControllerTest\UnsecuredController;
|
use SilverStripe\Control\Tests\ControllerTest\UnsecuredController;
|
||||||
|
use SilverStripe\Control\Tests\RequestHandlingTest\HTTPMethodTestController;
|
||||||
use SilverStripe\Dev\FunctionalTest;
|
use SilverStripe\Dev\FunctionalTest;
|
||||||
use SilverStripe\Security\Member;
|
use SilverStripe\Security\Member;
|
||||||
use SilverStripe\View\SSViewer;
|
use SilverStripe\View\SSViewer;
|
||||||
@ -34,6 +35,7 @@ class ControllerTest extends FunctionalTest
|
|||||||
ContainerController::class,
|
ContainerController::class,
|
||||||
HasAction::class,
|
HasAction::class,
|
||||||
HasAction_Unsecured::class,
|
HasAction_Unsecured::class,
|
||||||
|
HTTPMethodTestController::class,
|
||||||
IndexSecuredController::class,
|
IndexSecuredController::class,
|
||||||
SubController::class,
|
SubController::class,
|
||||||
TestController::class,
|
TestController::class,
|
||||||
@ -493,4 +495,15 @@ class ControllerTest extends FunctionalTest
|
|||||||
$response = $this->get('ContainerController/subcontroller/substring/subvieweraction');
|
$response = $this->get('ContainerController/subcontroller/substring/subvieweraction');
|
||||||
$this->assertEquals('Hope this works', $response->getBody());
|
$this->assertEquals('Hope this works', $response->getBody());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSpecificHTTPMethods()
|
||||||
|
{
|
||||||
|
// 'GET /'
|
||||||
|
$response = $this->get('HTTPMethodTestController');
|
||||||
|
$this->assertEquals('Routed to getRoot', $response->getBody());
|
||||||
|
|
||||||
|
// 'POST ' (legacy method of specifying root route)
|
||||||
|
$response = $this->post('HTTPMethodTestController', ['dummy' => 'example']);
|
||||||
|
$this->assertEquals('Routed to postLegacyRoot', $response->getBody());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\Control\Tests\RequestHandlingTest;
|
||||||
|
|
||||||
|
use SilverStripe\Control\Controller;
|
||||||
|
use SilverStripe\Control\HTTPRequest;
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
|
||||||
|
class HTTPMethodTestController extends Controller implements TestOnly
|
||||||
|
{
|
||||||
|
private static $url_segment = 'HTTPMethodTestController';
|
||||||
|
|
||||||
|
private static $url_handlers = [
|
||||||
|
'GET /' => 'getRoot',
|
||||||
|
'POST ' => 'postLegacyRoot',
|
||||||
|
];
|
||||||
|
|
||||||
|
private static $allowed_actions = [
|
||||||
|
'getRoot',
|
||||||
|
'postLegacyRoot',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function getRoot(HTTPRequest $request)
|
||||||
|
{
|
||||||
|
return "Routed to getRoot";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postLegacyRoot(HTTPRequest $request)
|
||||||
|
{
|
||||||
|
return "Routed to postLegacyRoot";
|
||||||
|
}
|
||||||
|
}
|
@ -13,8 +13,8 @@ class TestFormHandler extends FormRequestHandler
|
|||||||
{
|
{
|
||||||
private static $url_handlers = array(
|
private static $url_handlers = array(
|
||||||
'fields/$FieldName' => 'handleField',
|
'fields/$FieldName' => 'handleField',
|
||||||
"POST " => "handleSubmission",
|
"POST /" => "handleSubmission",
|
||||||
"GET " => "handleGet",
|
"GET /" => "handleGet",
|
||||||
);
|
);
|
||||||
|
|
||||||
// These are a different case from those in url_handlers to confirm that it's all case-insensitive
|
// These are a different case from those in url_handlers to confirm that it's all case-insensitive
|
||||||
|
Loading…
x
Reference in New Issue
Block a user