doulao3905 2012-12-27 14:02
浏览 20

将用户提供的(但与模型无关)参数传递给控制器​​?

This issue came up while maintaining an application written in CakePHP 1.3. Please bear in mind that while I can and do read the docs, my experience with Cake is quite limited.

The application has a Widget model and a WidgetController. When editing a Widget, one option the user has is to massively import data into the Widget in one of three modes: add to, remove from, or replace the widget data with what is imported.

The current implementation is a total mess (there is an "edit" action which performs all the 10 or so different mutation functions a Widget supports; it decides what to do exactly by sniffing the parameters from the submitted form), so I broke the "massive stuff" into a new action:

function batch($id)
{
    // massively apply data to Widget $id; either add, remove or replace
}

This action is triggered by a form in the "edit" view:

// Only relevant elements shown
echo $form->create('Widget', array('enctype'=>'multipart/form-data', 'action' => 'batch')); ?>
echo $form->input('id');
echo $form->input('action', array('type'=>'select',
    'options'=>array(
        'append'=>__('Append', true), 
        'replace'=>__('Replace existing', true), 
        'delete'=>__('Delete specified', true)
    ));
echo $form->end(); ?>

As it stands, this form bundles the action parameter into the Widget array and the only way I can get hold of that from the controller is with $this->data['Widget']['action'].

This is ugly and semantically wrong, so I hope there's a better way to do it.

Ideally I 'd want to submit to the URL /widget/batch/X/append, but that's not possible because the append part is not fixed. So I 'd settle with any of these:

  • somehow pass the action as a controller parameter like $id
  • somehow pass the action as a named parameter, allowing $this->params['named']['action']
  • some other way that does not require installing a custom route and does not require JavaScript

Can Cake 1.3 do this?

  • 写回答

1条回答 默认 最新

  • dtc9222 2012-12-28 19:42
    关注

    No

    no you cannot route a request to a different action based on a post parameter, nor with php alone change the action of a form. This is likely to be the same with any framework.

    Ideally I'd want to submit to the URL /widget/batch/X/append, but that's not possible because the append part is not fixed.

    The only way to do that is with javascript - of course it's not hard to write some appropriate js to do that.

    However

    If you don't want to use javascript to manipulate the form action when the user changes their selection, or just want the simplest solution - you can use setAction to internally redirect the request to more appropriate, dedicated, controller actions:

    function batch() {
        if (!empty($this->data['Widget']['action'])) {
            $this->setAction($this->data['Widget']['action']);
        }
    }
    
    function append() {
        if ($this->data) {
            if ($this->Widget->append($this->data)) {
                $this->Session->setFlash('Widget appended successfully');
            } else {
                $this->Session->setFlash('Error: Unable to append widget');
            }
        }
    }
    

    In this way you can likely refactor the existing code into more manageable and logical methods.

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂