如何在silverstripe 4中的控制器中运行?

时间:2018-01-03 13:04:46

标签: php silverstripe silverstripe-4

有人可以向我解释一下在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'动作之后。它不是执行find动作,而是调用errorPageController ...

1 个答案:

答案 0 :(得分:1)

看起来像:

  • $ url_handlers更像是动作的别名,而且
  • 行动,即使它有别名&#39;在$ url_handlers中总是需要添加到$ allowed_actions

我认为它的作用如下: &#39;别名&#39;对于该操作应该添加到$ allowed_actions然后它应该在$ url_handlers中映射到action方法

但它的作用如下: &#39;别名&#39;对于动作应该在$ url_handlers中映射到action方法,然后将action方法添加到$ 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