Dynamic first URI segment? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Dynamic first URI segment? (/showthread.php?tid=20625) |
Dynamic first URI segment? - El Forum - 07-15-2009 [eluser]NateL[/eluser] I'm trying to wrap my head around how I would make the first and second segments of a URI dynamic. For instance: example.com/*clientname* my client goes to a designated section of my site that I dynamically set up via an Admin control panel. To take it a step further: example.com/clientname/*projectname* So, now we have the segment 1 looking for the name of the client, and segment 2 looking for a project related to that client. I'm reading over the documentation, and I'm thinking it has something to do with URI Wildcards. *thinking...thinking* Thanks Dynamic first URI segment? - El Forum - 07-16-2009 [eluser]bigtony[/eluser] If if doesn't have to be the first and second segments you can do it by creating a contoller called 'Clients' and using the _remap method to capture the client name, etc. Code: function _remap($clientname, $projectname) { If you have to use the first & second segments you could try using the config/routes.php file to 'silently' call a controller using something like: Code: $route['(:any)/(:any)'] = "controller_name/$1/$2"; Dynamic first URI segment? - El Forum - 07-16-2009 [eluser]Michael Wales[/eluser] You will create a controller/method to handle this - let's just say the user has already logged in and the url example.com/walesmd/codeigniter will take me to my dashboard which is found at Projects->index(). You would establish the following route: Code: $route['[(a-zA-Z0-9-)+]/[(a-zA-Z0-9-)+]'] = 'projects/index/$1/$2'; Which will direct the request to: Code: class Projects extends Controller { Now, the only thing you have to worry about is your non-dynamic URIs. Let's say you have an about page at example.com/about that points to Pages->about(). The previous routing rule will instead send that request to Projects->index('about') - not what we want. So, for all of your non-dynamic routes you will need to create a routing rule as well, higher in the list than the dynamic route: Code: // This list should go from most specific to least specific, as top rules take precedence. Dynamic first URI segment? - El Forum - 07-16-2009 [eluser]NateL[/eluser] Thanks guys, it makes sense. I was curious about non-dynamic URLs, the main one being example.com/admin - so thanks for mentioning that. Dynamic first URI segment? - El Forum - 07-23-2009 [eluser]NateL[/eluser] SWEET! It's working! I need to touch up on my Regular Expression skillz, but your regex didn't work for me. I copied what was in the user guide, but now it only works with lowercase letters. $route['([a-z]+)/([a-z]+)'] = 'projects/index/$1/$2'; $route['([a-z]+)'] = 'client/index/$1'; example.com/client/project will pull up all of the files for that project example.com/client will pull up all of the projects for that client nifty This is SO much better than the way I did it before....yeh, I programmed PHP to create physical directories, and copy an index.php file in there (yikes...) Dynamic first URI segment? - El Forum - 07-23-2009 [eluser]NateL[/eluser] Oh - I figured it out. Your brackets and parenthesis are swapped and in the wrong place. fixed: Code: $route['([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)'] = 'projects/index/$1/$2'; Dynamic first URI segment? - El Forum - 07-24-2009 [eluser]NateL[/eluser] Another question about this... Is it possible to tell CI that if you're looking in the Admin directory - NOT to follow that regular expression? example.com/admin - I use $route['admin'] = "admin/admin/index"; the problem I just ran into was if I browse to example.com/admin/addressbook, my routing rule was not set right and it looked for the client "admin" and the project "addressbook" I want to tell CI that ANYthing after admin/ is not to be searched as client/project If I go to admin/foobar - and foobar isn't an existing controller, an error is thrown. Dynamic first URI segment? - El Forum - 07-24-2009 [eluser]NateL[/eluser] woot!! Got it $route['admin/(:any)'] = "admin/$1"; |