开发者

posting to same page good practice?

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?


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.


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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜