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 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题