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

could someone please explain me how actions works in silverstripe 4?

I have controller like this:

<?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';
    }
}

When i launch it in browser i got this:

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.

I don't understand why after RequestHandler matches rule 'add-shop' with action 'shopForm' controller didn't execute 'shopForm' action. Instead of execute found action it called errorPageController...

It looks like:

  • $url_handlers works more like alias for actions and that's it
  • action, even if it has 'alias' in $url_handlers always needs to be added to $allowed_actions

I thought that it works like: 'alias' for the action should be added to $allowed_actions and then it should be mapped in $url_handlers to action method

But it works like: 'alias' for the action should be mapped in $url_handlers to the action method and then action method should be added to $allowed_actions

so basically for the code:

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';
    }
}

We have: for 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

and for 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