¿Enrutar cadenas aleatorias a un controlador específico en CodeIgniter?

I am trying to create short links to my application in codeigniter but I've met a kind of a problem when designing my route. The problem is that I want a route which will take a string containing a-Z and numbers and redirect that to a controller called image with the string after. Like this: app.com/randomstring -> app.com/image/randomstring. But when I am trying to do this in the routes config file with a regular expression it disables my application and I am unable to enter "normal" urls with controllers that already exist.

How my route looks like right now (I know it's probably very wrongly made):

$route['(^[A-Za-z0-9]+$)'] = "image/$1";

Is there any easy way to redirect with that short url without using another fake controller first like this: app.com/i/randomstring -> app.com/image/randomstring

And could you maybe help me improve and tell me what part of my regexp is failing?

preguntado el 10 de marzo de 12 a las 11:03

Worth noting is that I have URL rewriting in .htaccess to remove index.php from any page. -

Are the strings truly random? If so, it's possible (although unlikely) that they could conflict with a valid controller name. What exactly do the image urls look like; how are you creating them? Are they guaranteed to have at least one number, length boundaries, mixed case, etc.? -

Well, I will make sure that the string do not conflict with any controller name. I am creating them with a function that takes a number and just convert it to a string. Right now I am using this guy's function to do that, but that may change in the future. kevin.vanzonneveld.net/techblog/article/… -

So, the strings may not include a number, and they will be short in the beginning but longer as the numbers of entries in the database increase. It's mixed case, as you can see from the link. But my problem is that I can't link it without getting problem with my normal controllers. -

I understand your problem fully, but without a clearly defined spec on what the image urls will be, there's no comprehensive way to solve this. Even YouTube uses urls like /watch?v=h8skj3, the "watch" is the trigger. Using a i/r4nd0m$tring would make this a non-issue. I suppose you could define every possible exception as well. -

1 Respuestas

As I mentioned in the comments, without a clearly defined spec on what the image urls will be, there's no comprehensive way to solve this. Even YouTube (related to the library you linked to) uses urls like /watch?v=h8skj3, where "watch" is the trigger.

El uso de un i/r4nd0m$tring would make this a non-issue, and it's what I suggest, but I had another idea:

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

// Re-Route all valid controllers
foreach (array('users', 'login', 'blog', 'signup') as $controller)
{
    $route[$controller] = $controller;
    $route[$controller.'/(:any)'] = $controller.'/$1';
}
unset($controller);

You might need the image route last, I'm not 100% sure. This should route todo a image/ excepto the controllers you define. You could even use glob() or something to scan your controller directory for PHP files to populate the array.

Another way to get one character shorter than i/string could be to use a character trigger, like example.com/*randomstring, but that's a little silly, i/ is much cleaner and obviously, easier to deploy.

respondido 10 mar '12, 15:03

Thanks this is a perfect solution for my application. I will probably deploy this when all the controllers are written and ready so I won't do any mistake of making one missing the the array. - Ms01

Just reminded myself that I could write some code to check for all the files in the controller-directory. scandir would probably work. :) - Ms01

Just as long as you're sure no urls will match a controller name. I still say i/string is better. If you decide later that you need videos, then you wouldn't be able to do it without v/string. Same if you want to route urls or something else later. Your "random namespace" would be owned by images already. - Wesley Murch

yeah that's true. I guess I'll stick with /i/string then. Thanks for the solution anyway. - Ms01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.