duanlan5320 2018-01-03 13:04
浏览 21

如何在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...

  • 写回答

1条回答 默认 最新

  • duanchu2607 2018-01-03 15:23
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测