Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>Alright, let's pick this apart.</h3> <p><strong>Ignore CodeIgniter's reserved routes.</strong></p> <p>The <code>default_controller</code> and <code>404_override</code> portions of your route are unnecessary. Routes are compared to the requested URI to see if there's a match. It is highly unlikely that those two items will ever be in your URI, since they are special reserved routes for CodeIgniter. So let's forget about them.</p> <pre><code>$route['(?!home|activity)[A-Za-z0-9][A-Za-z0-9_-]{2,254}'] = 'view/slug/$1'; </code></pre> <p><strong>Capture everything!</strong></p> <p>With regular expressions, a group is created using parentheses <code>()</code>. This group can then be retrieved with a back reference - in our case, the <code>$1, $2, etc.</code> located in the second part of the route. You only had a group around the first set of items you were trying to exclude, so it would not properly capture the entire wild card. You found this out yourself already, and added a group around the entire item (good!).</p> <pre><code>$route['((?!home|activity)[A-Za-z0-9][A-Za-z0-9_-]{2,254})'] = 'view/slug/$1'; </code></pre> <p><strong>Look-ahead?!</strong></p> <p>On that subject, the first group around <code>home|activity</code> is not actually a traditional group, due to the use of <code>?!</code> at the beginning. This is called a <a href="http://www.regular-expressions.info/lookaround.html" rel="noreferrer">negative look-ahead</a>, and it's a complicated regular expression feature. And it's being used incorrectly:</p> <blockquote> <p><em>Negative lookahead is indispensable if you want to match something not followed by something else.</em></p> </blockquote> <p>There's a LOT more I could go into with this, but basically we don't really want or need it in the first place, so I'll let you explore if you'd like.</p> <p>In order to make your life easier, I'd suggest separating the home, activity, and other existing controllers in the routes. CodeIgniter will look through the list of routes from top to bottom, and once something matches, it stops checking. So if you specify your existing controllers <em>before</em> the wild card, they will match, and your wild card regular expression can be greatly simplified.</p> <pre><code>$route['home'] = 'pages'; $route['activity'] = 'user/activity'; $route['([A-Za-z0-9][A-Za-z0-9_-]{2,254})'] = 'view/slug/$1'; </code></pre> <p>Remember to list your routes in order from most specific to least. Wild card matches are less specific than exact matches (like home and activity), so they should come after (below).</p> <h3>Now, that's all the complicated stuff. A little more FYI.</h3> <p>Remember that dashes <code>-</code> have a special meaning when in between <code>[]</code> brackets. You should escape them if you want to match a literal dash.</p> <pre><code>$route['([A-Za-z0-9][A-Za-z0-9_\-]{2,254})'] = 'view/slug/$1'; </code></pre> <p>Note that your character repetition min/max <code>{2,254}</code> only applies to the second set of characters, so your user names must be 3 characters at minimum, and 255 at maximum. Just an FYI if you didn't realize that already.</p> <p>I saw your own answer to this problem, and it's just ugly. Sorry. The <code>^</code> and <code>$</code> symbols are used improperly throughout the lookahead (which still shouldn't be there in the first place). It may "work" for a few use cases that you're testing it with, but it will just give you problems and headaches in the future.</p> <p>Hopefully now you know more about regular expressions and how they're matched in the routing process.</p> <p>And to answer your question, no, you should not use <code>^</code> and <code>$</code> at the beginning and end of your regex -- CodeIgniter will add that for you.</p> <hr> <h3>Use the 404, Luke...</h3> <p>At this point your routes are improved and should be functional. I will throw it out there, though, that you might want to consider using the controller/method defined as the <code>404_override</code> to handle your wild cards. The main benefit of this is that you don't need ANY routes to direct a wild card, or to prevent your wild card from goofing up existing controllers. You only need:</p> <pre><code>$route['404_override'] = 'view/slug'; </code></pre> <p>Then, your View::slug() method would check the URI, and see if it's a valid pattern, then check if it exists as a user (same as your slug method does now, no doubt). If it does, then you're good to go. If it doesn't, then you throw a 404 error.</p> <p>It may not seem that graceful, but it works great. Give it a shot if it sounds better for you.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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