douyue1481 2015-11-25 08:03
浏览 55
已采纳

slug在codeigniter中无法正常工作

I am creating pages with slug in codeigniter. But it is not working if my slug in database have slashes.

like -:

1) con_r/this //not working

2) con_q //working

3) con_r/this/that //not working

Take a look at my code

routes.php

$route['default_controller'] = "home";
$route['404_override'] = 'home';
$route['translate_uri_dashes'] = TRUE;

require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$query = $db->get('slugu');
$result = $query->result();
foreach( $result as $row ){
    $route[ $row->slug ] = 'page/call/'.$row->slug;
}

Page //controller

 public function call($slug){

    $data['page_base'] = $this->site_model->get_page_info($slug);

    /*call slider*/
    echo $this->call_slider($slug);

    if (empty($data['page_base'])){
        show_404();
    }

    print_R($data['page_base']);
    die;
}
  • 写回答

1条回答 默认 最新

  • dtz55359 2015-11-25 09:59
    关注

    This is due to the way CodeIgniter handles URL's. The first two URL segments is the class and function. The third and any more segments are passed into the function as variables. So in your example the following URL works because the full slug is passed into the function:

    example.com/page/call/con_q
    
    public function call($slug) {
      echo $slug; // con_q
    }
    

    Whereas the following does not because only the first segment of the slug is passed into the function

    example.com/page/call/con_r/this
    
    public function call($slug) {
      echo $slug; // con_r NOT con_r/this
    }
    
    public function call($segment_1, $segment_2) {
      echo $segment_1; // con_r
      echo $segment_2; // this
    }
    

    A solution which I think would work would be the following. Please note that this is untested and may need some modifications.

    example.com/page/call/con_r/this
    
    public function call() {
      $slug = func_get_args();
      $slug = implode('/', $slug);
      echo $slug; // con_r/this
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示