Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP MVC design - multiple actions to same url/controller
    primarykey
    data
    text
    <p>In a MVC pattern, what's the best way to handle when a single view could have multiple actions of the same type (e.g POST)?</p> <p>Say for instance in a TODO list application. You might allow a user to create multiple lists. Each list could have multiple items. So a user navigates to site.com/list/1 which shows them all the items on the 1st list (1 is a GET parameter). There are then 2 forms (POST) on this page to allow a user to:</p> <ol> <li>Create a new item</li> <li>Delete an existing item</li> </ol> <p>Should the bootstrap create a "listcontroller", inspect the POST variables and then call the appropriate method similar to :</p> <pre><code>$lc = new ListController(); if(strtolower($request-&gt;verb) === 'post'): if(isset($_POST['title'])) : $data = $lc-&gt;newItem($_POST); $load-&gt;view('newitem.php', $data); else if(isset($_POST['delete']) &amp;&amp; isset($_POST['id'])): $data = $lc-&gt;deleteItem($_POST); $load-view('deleteitem.php', $data); endif;// End if post title else: //GET request here so show view for single list endif; // </code></pre> <p>Or is it better to just do something like</p> <pre><code>$lc = new ListController(); if(isset($_POST)): //controller handles logic about what function to call $data = $lc-&gt;PostAction($_POST); // $data could also potentially hold correct view name based on post $load-&gt;view(); else: //again just show single list endif; </code></pre> <p>I'm just struggling how best to have a controller potentially handle multiple different actions, as there's potentially quite a few nested if/else or case statements to handle different scenarios. I know these would have to sit somewhere, but where is cleanest? </p> <p>I know that there are many frameworks out there, but I'm going through the whole "want to understand best practice" behind it phase. Or is this totally the wrong way to do it? Should the controllers actually be structured differently?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload