Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>1) So the main question, does anything just not look right?</em></p> <p>Personally, I see this becoming more complicated as your site grows. An MVC framework, as I was taught, should pretty much be "Set it and forget it" – you're separating the request handler (controller) from the database querying and business end (model) and from the display elements (view).</p> <p>[<strong><em>NB:</em></strong> You may need other core aspects. My standard framework includes some core elements that carry the session through the various parts, as well as handle fundamental aspects of the site work – For instance, while the models are responsible for making the right database calls as directed by the controller, there are core functions in a <code>sql.class.php</code> file that give me a standardized set of methods for making those calls and delivering or caching the results as needed.]</p> <p>Your dispatch method is on the right track with this – you're extracting from the URI the name of the controller (Forums, Profiles, etc.). Do you need a uri map? I feel you're creating an unnecessary situation in which you have to update this map each time, rather than simply creating a new controller when you need new functionality, and registering it with the database. I'm not saying you're wrong per se, I just don't feel I'd have done it that way.</p> <p><em>2) Is there a better way to detect what is in the URI than using the regex on an array like I am doing, consider it on a high traffic site?</em></p> <p>Control the outcome (no pun intended, since it's the controller that does the work here). Consider this approach, and see how it works for you:</p> <p>Your index.php file (aka "Main Controller") grabs the URI and explodes the values along "/" into bits. bit[0] is the controller ID – this says "I want to use the controller named bit[0]=>value". This is done as:</p> <pre><code>require_once( dirname( __FILE__ )."/controllers/".$bit[0]."controller.php" ); </code></pre> <p>Personally, being a bit of a neat freak when it comes to directory structures, I use bit[0] to identify the directory in which controller.php is located, as I might have sub controllers.</p> <p>It's this controller file that I use to parse other bits. For this, I'll use an example:</p> <p>Assume that bit[0] carried the value "forums". I might pass, if it's set, bit[1] to a switch statement. By default, I always want to list, but I might specifically direct it to "list", "view", or "post" in bit[1]. This will tell me in the controller class which method to call. The method will then tell me to call the associated "forums" model if I need to perform queries and cache the forum listing, for instance.</p> <p>The extraneous "bits" may do one of two things: they may be passed as simple arguments to the method as to what data to request from the model, or bit[1] may be complex enough to warrant a sub controller, and the subsequent bits will be passed to that controller to determine the appropriate action, as was done with the forums controller.</p> <p>Regex, being slow, should be avoided when possible. Since we may have a URI of <code>/forums/view/102305</code> we can assume that the <code>forums</code> controller will be passing <code>102305</code> to the method associated with the <code>view</code> argument (the method being something like <code>private function displayPost( $id )</code> where <code>$id</code> is <code>102305</code>). No regex is needed since we can simply explode the values along a common anticipated delimiter.</p> <p><em>3) Since everything is routed through the index.php file with this, how would I go about handling AJAX requests?</em></p> <p>Not terribly difficult. If the controller is set to, say, AJAX, you could rebuild the URL and direct access it. You could write exclusions in the .htaccess file (<code>RewriteRule ^(AJAX)($|/) - [L]</code>). Or (not ideal, but a sneaky workaround) is to append <code>../</code> to your AJAX URI to push the URI back to root – it's no longer attempting to access <code>index.php</code> so the rewrite rule doesn't apply.</p> <p><strong>Edit</strong></p> <p>Let's assume that we're using a URI of <code>/forums/id-1234/page-4</code> per your example. Again, let's assume as I mentioned above that <code>forums</code> refers to the controller to be used, and every other <code>/</code> delimits arguments (what I like to call "drill downs"). So, in our forum controller file (let's call it <code>forumcontroller.php</code>, we might have something like this (extremely simplified) constructor:</p> <pre><code>// $registry is a class containing fundamental methods, and is meant to exemplify all // classes tied to the main controller "index.php". Keep in mind, I'm assuming we've // found the right controller by exploding the URI, and passed the remainder as bits // to the constructor. public function __construct( registry $registry ) { $this-&gt;registry = $registry; //tying this controller to main controller. // For ease and clarity, we're assuming there's no case in which you wouldn't have // bits set. Error checking is easy. $bits = $this-&gt;registry-&gt;getURLBits; switch( $bits[0] ) { case 'view': $this-&gt;showForumEntry( $bits[1], (isset( $bits[2] ) ? $bits[2] : '' ); break; case 'edit': $this-&gt;editForumEntry( $bits[1] ); break; case 'post': $this-&gt;postForumEntry(); break; default: $this-&gt;listForumEntries(); break; } } private function showForumEntry( $thread, $offset ) { // Because you wanted to prepend id to the id element, we can use this for // cheekiness in the query if our DB is well designed. $data = explode('-', $thread); // Select all from forums where id = 1234 $sql = "SELECT * FROM forums WHERE $data[0] = $data[1]"; if( $offset != '' ) { $page = explode('-', $offset); $offset = $page[1] * 25; // Or whatever your max per page is. Make it dynamic. $max = $offset+25; $sql .= " LIMIT $offset, $max"; } // You see where I'm going with this... } </code></pre> <p>The point is that you're in control of what is being passed and how it gets handled. Control the URIs and you can simplify their processing.</p> <p><strong>Edit 2</strong> Reading through again, there's a few concepts that I think will help you and that you should familiarize yourself with:</p> <p>View the "Factory" pattern here (My $registry is, at it's heart, a set of factories): <a href="http://php.net/manual/en/language.oop5.patterns.php" rel="nofollow">http://php.net/manual/en/language.oop5.patterns.php</a> </p> <p>A good breakdown of MVC graphically: <a href="http://best-practice-software-engineering.ifs.tuwien.ac.at/patterns/images/mvc3.jpg" rel="nofollow">http://best-practice-software-engineering.ifs.tuwien.ac.at/patterns/images/mvc3.jpg</a></p> <p>More on Factory methods: <a href="http://www.devshed.com/c/a/PHP/Design-Patterns-in-PHP-Factory-Method-and-Abstract-Factory/" rel="nofollow">http://www.devshed.com/c/a/PHP/Design-Patterns-in-PHP-Factory-Method-and-Abstract-Factory/</a></p> <p>One other note, and this is a personal observation after having worked with Joomla, Drupal, Wordpress, and various different enterprise CMS and BBS solutions – Design solely with you in mind. When you start trying to become "something for everybody", you end up with a lot of unnecessary bloat that's getting loaded with each page and used 1 time out of 100. MVC is a design pattern, and using it as a pattern will tell you to get rid of excess in every aspect, including URIs. Processing <code>/controller/arg1-Identifier/arg2-offset</code> is unnecessary, and you can easily get away with <code>/controller/id/offset</code> (e.g. <code>/forums/1234/4</code>). If you want to make it SEO friendly, add the thread title, not a tag identifying the ID (e.g. <code>/forums/1234-This-Is-A-Topic/4</code>).</p> <p>Now, let's also point out the obvious about my edit above – This is a controller intended solely for the forum element. Each element of your site (i.e. forums, galleries, profiles, etc.) should have it's own controller. Why? Because each is doing completely different things on its pages. So make use of that – you don't need to use a URI map so long as you understand that you're directing to the controller, and the controller is delegating responsibilities to the model and any sub controllers that might be needed.</p> <p>I really hope that helps.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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