SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    specific mvc question (redirects)

    Hi all,

    I found this great forum two weeks ago and i within those two weeks I have learned more than in the last two years ... Thanks for that!

    Now I have one specific mvc question, regarding redirects within a controller .. for example I have a controller 'Controllers_Comments_Save' that firstly validates the user input and -if validated- saves the comment with the help of a CommentModel to the database. Now when the validation fails, i have to redirect back to the form. This is my question: Should I redirect with a header to the news-form or simply generate a new NewsViewController (that shows the form) within the CommentsController and then apply the View from the NewsViewController to the View of the CommentsController? (If you havent understand my problem because of my bad english i have some code for you: (simplified code))

    Method 1:
    PHP Code:
    //CommentsController
    ...
    if(
    $form->valid() { save_comment(); }
    else { 
    redirect("news_view"); } 
    Method 2:
    PHP Code:
    //CommentsController
    ...
    if(
    $form->valid() { save_comment(); }
    else {
    $NewsController = new NewsViewController();
    $NewsController->run($errors);
    $this->view $NewsController->getView(); 

    I am currently using Method 2 (because i can simply pass the errors of the form as a parameter while i have to save the errors in the Session-Array in Method 1) but i wondered whether it is 'valid' mvc?!
    Thanks for all your answers.

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Why do you instantiate another controller class rather than the view directly? That just creates an unnecessary dependency between the controllers?
    Saul

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, if I just created the View I had to copy code from the newsController to the commentsController, but isn't it a big goal in mvc to avoid code duplication?

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    True. What do you think about a hierarchy of controllers? In this case the Save controller would be a child of the NewsView controller. After the failed validation the Save controller would just pass the handling of request through to parent controller which displays the form.
    Saul

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I really like that idea. Thanks for your great suggestions, I will try to realize this now

  6. #6
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Look at the Chain Of Responsibility, to allow you to move from one controller, to another based on events, such as success and fail


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •