0

有人可以解释一下 Silverstripe 4 中的操作是如何工作的吗?

我有这样的控制器:

<?php

use SilverStripe\Control\HTTPRequest;

class ShopHolderPageController extends PageController
{
    private static $allowed_actions = [
        'add-shop'
    ];

    private static $url_handlers = [
        'add-shop' => 'shopForm'
    ];

    protected function init()
    {
        parent::init();
    }

    public function shopForm(HTTPRequest $request)
    {
        return 'test';
    }
}

当我在浏览器中启动它时,我得到了这个:

Debug (line 261 of RequestHandler.php): Testing 'add-shop' with 'add-shop' on ShopHolderPageController

Debug (line 271 of RequestHandler.php): Rule 'add-shop' matched to action 'shopForm' on ShopHolderPageController. Latest request params: array ( )

Debug (line 261 of RequestHandler.php): Testing '$Action//$ID/$OtherID' with '' on SilverStripe\ErrorPage\ErrorPageController

Debug (line 271 of RequestHandler.php): Rule '$Action//$ID/$OtherID' matched to action 'handleAction' on SilverStripe\ErrorPage\ErrorPageController. Latest request params: array ( 'Action' => NULL, 'ID' => NULL, 'OtherID' => NULL, )

Debug (line 185 of RequestHandler.php): Action not set; using default action method name 'index'

Debug (line 234 of Controller.php): Request handler returned HTTPResponse object to ShopHolderPageController controller;returning it without modification.

我不明白为什么在 RequestHandler 将规则“add-shop”与操作“shopForm”匹配之后,控制器没有执行“shopForm”操作。它没有执行找到的操作,而是调用 errorPageController...

4

1 回答 1

2

看起来像:

  • $url_handlers 更像是操作的别名,仅此而已
  • 动作,即使它在 $url_handlers 中有“别名”,也总是需要添加到 $allowed_actions

我认为它的工作方式如下:应该将操作的“别名”添加到 $allowed_actions 然后它应该在 $url_handlers 中映射到操作方法

但它的工作原理是:动作的“别名”应该在 $url_handlers 中映射到动作方法,然后动作方法应该添加到 $allowed_actions

所以基本上对于代码:

use SilverStripe\Control\HTTPRequest;

class ShopHolderPageController extends PageController
{
    private static $allowed_actions = [
        'shopForm'
    ];

    private static $url_handlers = [
        'add-shop' => 'shopForm',
        'edit-shop/$ID' => 'shopForm'
    ];

    protected function init()
    {
        parent::init();
    }

    public function shopForm(HTTPRequest $request)
    {
        var_dump($request->param('ID'));
        return 'test';
    }
}

我们有:对于 localhost/shops/add-shop?debug_request=1

Debug (line 261 of RequestHandler.php): Testing 'add-shop' with 'add-shop' on ShopHolderPageController

Debug (line 271 of RequestHandler.php): Rule 'add-shop' matched to action 'shopForm' on ShopHolderPageController. Latest request params: array ( )

NULL test

对于 localhost/shops/edit-shop/12?debug_request=1

Debug (line 261 of RequestHandler.php): Testing 'add-shop' with 'edit-shop/12' on ShopHolderPageController

Debug (line 261 of RequestHandler.php): Testing 'edit-shop/$ID' with 'edit-shop/12' on ShopHolderPageController

Debug (line 271 of RequestHandler.php): Rule 'edit-shop/$ID' matched to action 'shopForm' on ShopHolderPageController. Latest request params: array ( 'ID' => '12', )

string(2) "12" test
于 2018-01-03T15:23:12.650 回答