发布到同一页面的好习惯?

发布于 2024-10-04 21:30:34 字数 1073 浏览 2 评论 0原文

我最近一直在阅读许多不同的内容,以提高我的编码质量。

我想知道将表单发布到同一页面是否是一个好习惯,我经常在当前的应用程序中使用这种类型的代码片段。

public function addAction(){
    $form = new Application_Form_Add();

    $this->view->form = $form;

    if(!$this->_request->isPost() && $form->isValid($this->_request->getParams())){
        $mapper = new Application_Model_ModelMapper();
        $model = new Application_Model_Model($form->getValues());

        if(!$mapper->save($model)){
            $this->view->messages('an error occurred etc ... ');
            return;     
        }

        $this->_helper->redirector->gotoRoute(array('id' => $model->id), 'model_view');
    }
}

所以我的添加操作在 GET 请求上显示表单并在 POST 请求上处理它。

我看到了 Matthew Weier 的这篇 文章 O'Phinney,我想每个人都同意他是当今的 PHP 大师之一。在他的例子中,他做了两种不同的动作,一种是显示形式,一种是处理形式。因此,如果表单未验证,他会使用 $this->render 重新渲染表单视图。

将表单提交到同一页面是一种不好的做法吗?如果是,为什么?

I have been doing some reading about many different things lately to improve my coding quality.

I am wondering if it is a good practice to post form to the same page often I used this type of snippet in my current applications.

public function addAction(){
    $form = new Application_Form_Add();

    $this->view->form = $form;

    if(!$this->_request->isPost() && $form->isValid($this->_request->getParams())){
        $mapper = new Application_Model_ModelMapper();
        $model = new Application_Model_Model($form->getValues());

        if(!$mapper->save($model)){
            $this->view->messages('an error occurred etc ... ');
            return;     
        }

        $this->_helper->redirector->gotoRoute(array('id' => $model->id), 'model_view');
    }
}

so my add action presents the form on GET request and process it on POST request.

I came around this article of Matthew Weier O'Phinney, I think everyone can agree he is one of the PHP gurus of the moment. In his example, he make two differents action one to show the form, one to process it. So if the form doesn't validate he plays with $this->render to re render the form view.

Is it a bad practice to submit form to same page and if yes why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜空下最亮的亮点 2024-10-11 21:30:34

出于代码可读性和可维护性的目的,两个不同的控制器操作(尽管它们与相同的表单相关联)最好分离为两个函数/脚本。

想象一下,如果所有 CRUD 操作都组合在一个函数/脚本中会是什么样子。那会很混乱。

For the purpose of code readability and maintainability, two different controller actions - despite them associated with the same form - are better separated as two functions/scripts.

Imagine what it would be like if all CRUD operations were combined in a single function/script. That will be messy.

梦里的微风 2024-10-11 21:30:34

我不认为这有什么坏处。

无论我们对同一页面和不同页面执行此操作。

两种方法在各自的位置上都是正确的。

使用单页是一种排序和甜蜜的方式,使用 2 页为我们的代码提供了一种托管方式。

在我看来,两者是平等的。

I don't think it's bad.

No matter we do this with same page and different page.

Both ways are correct at their places.

using singe page is a sort and sweet way and using 2 pages gives our code a managed way.

In my opinion both are equall.

葬花如无物 2024-10-11 21:30:34

看来,我更喜欢使用一个动作来实现它。这将所有的东西放在一起。 O'Phinney 的控制器可以像这样:

  • addAction (使用 saveAction 来保存)
  • editAction (使用 saveAction 来保存)
  • saveAction (不呈现自身)

为所有控制器保存/编辑逻辑提供一种方法是唯一的原因(假设!)它和额外的行动。很酷,将所有东西都放在一处。但是:这需要编辑和添加两者具有完全相同的逻辑。一旦有任何if/else喜欢

public function saveAction()
{
    if($action == 'edit)
    {
        // edit logic 
    } else if($action == 'add')
    {
        // add logic
    }
}

根据操作有不同的逻辑,这完全利用了一个操作的想法。因此,如果您知道逻辑始终相同并且不太可能更改,那么这就是节省大量编码工作的方法:)。

另一方面,如果您的添加/编辑逻辑有点不同,我会将所有逻辑放在一起:

  • addAction (打印表单并在 POST 上保存)
  • editAction (打印表单并在 POST 上保存)

这可能看起来像代码-重复(也许是),但是,我的编辑/添加操作大约有 3-6 行代码。但一旦其中任何一个的逻辑发生变化,你就真正了解这个地方了。

所以这是个人意见,一旦事情变得更加复杂,我就会坚持采取单独的行动。

(请注意:对于逻辑,我指的是控制器逻辑,所有数据逻辑无论如何都应该在模型中)

In my oppinion I prefer to use one action for it. This keeps all the stuff together. O'Phinney's controller could loo like this:

  • addAction (uses saveAction to save)
  • editAction (uses saveAction to save)
  • saveAction (does not render itself)

Having one method for all controller save/edit logic is the only reason (assumption!) to make it and extra action. Cool, keeps everything in one place. But: this required both, edit and add, to have exactly the same logic. As soon as there is any if/else like

public function saveAction()
{
    if($action == 'edit)
    {
        // edit logic 
    } else if($action == 'add')
    {
        // add logic
    }
}

to have a different logic depending on the action, this totally exploits the idea of one action. So if you know that the logic is always the same and is very unlikly to change, this is the way to save you lot's of coding work :).

On the other hand, if your add/edit logic are a bit different, I'd keep all logic together that belongs together:

  • addAction (prints form and saves on POST)
  • editAction (prints form and saves on POST)

This might look like code-duplication (and maybe is), but then, my edit/add actions have about 3-6 lines of code. But as soon as the logic of either changes, you really know the place.

So this is a personally oppinion, I'd stick with seperated actions as soon as stuff becomes more complex.

(As a note: to logic I'm refering to controller logic, all data-logic should be in the model anyway)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文