CakePHP博客教程编辑帖子 - 如何将提交的数据用于编辑操作?

时间:2014-10-12 07:58:05

标签: cakephp-2.3

当'编辑'单击索引视图上的按钮,数据首先提交给PostsController Edit动作。由于'设置'在该操作中未调用函数,如何呈现edit.ctp视图并将编辑后的数据发回到编辑操作?

1 个答案:

答案 0 :(得分:0)

您应该粘贴控制器中的代码,以便人们可以准确地看到您正在谈论的内容。

假设您有一个非常标准的编辑方法,它应该包含这样的行:

$this->request->data = $this->Post->read(null, $id)

因此,设置了$this->request->data。 Cake确保整个$this->request在视图中可用。在View超类中,有一个名为$_passedVars的数组,其中包括request。在视图的__construct中,它将控制器中$_passedVars中指定的每个变量复制到视图中。

如果您想查看详细信息,请查看/lib/Cake/View/View.php,特别是$_passedVars变量以及__construct方法的前几行:

/**
 * List of variables to collect from the associated controller.
 *
 * @var array
 */
    protected $_passedVars = array(
        'viewVars', 'autoLayout', 'ext', 'helpers', 'view', 'layout', 'name', 'theme',
        'layoutPath', 'viewPath', 'request', 'plugin', 'passedArgs', 'cacheAction'
    );

/**
 * Constructor
 *
 * @param Controller $controller A controller object to pull View::_passedVars from.
 */
    public function __construct(Controller $controller = null) {
        if (is_object($controller)) {
            $count = count($this->_passedVars);
            for ($j = 0; $j < $count; $j++) {
                $var = $this->_passedVars[$j];
                $this->{$var} = $controller->{$var};
            }
            $this->_eventManager = $controller->getEventManager();
        }
        if (empty($this->request) && !($this->request = Router::getRequest(true))) {
            $this->request = new CakeRequest(null, false);
            $this->request->base = '';
            $this->request->here = $this->request->webroot = '/';
        }
        if (is_object($controller) && isset($controller->response)) {
            $this->response = $controller->response;
        } else {
            $this->response = new CakeResponse();
        }
        $this->Helpers = new HelperCollection($this);
        $this->Blocks = new ViewBlock();
        $this->loadHelpers();
        parent::__construct();
    }