dongnao1908 2012-03-10 11:37
浏览 83
已采纳

将随机字符串路由到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?

  • 写回答

1条回答 默认 最新

  • dsgsdg206050 2012-03-10 14:09
    关注

    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.

    Using a 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 everything to image/ except 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:功能监听网页
  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群