Welcome Guest, Not a member yet? Register   Sign In
Dynamic first URI segment?

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.


Thanks Big Grin

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.
function _remap($clientname, $projectname) {
    // Validate client & project names (e.g. by database calls)
    // Do stuff... (e.g. call other methods as appropriate)
The url for the client is then: example.com/clients/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:
$route['(:any)/(:any)'] = "controller_name/$1/$2";
Not sure if above will work as is, you might need to experiment.

[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:

$route['[(a-zA-Z0-9-)+]/[(a-zA-Z0-9-)+]'] = 'projects/index/$1/$2';

Which will direct the request to:
class Projects extends Controller {
  function index($username, $project) {

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:

// This list should go from most specific to least specific, as top rules take precedence.
$route['about'] = 'pages/about';
$route['[(a-zA-Z0-9-)+]/[(a-zA-Z0-9-)+]'] = 'projects/index/$1/$2';
$route['[(a-zA-Z0-9-)+]'] = 'users/settings/$1';

Thanks guys, it makes sense.

I was curious about non-dynamic URLs, the main one being example.com/admin - so thanks for mentioning that.


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 Smile 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...)

Oh - I figured it out. Your brackets and parenthesis are swapped and in the wrong place.

$route['([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)'] = 'projects/index/$1/$2';
$route['([a-zA-Z0-9-]+)'] = 'client/index/$1';

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.


Got it Smile

$route['admin/(:any)'] = "admin/$1";

Theme © iAndrew 2016 - Forum software by © MyBB