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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 angular开发过程中,想要读取模型文件,即图1的335行,会报404错误(如图2)。但我的springboot里配置了静态资源文件,如图3。且在该地址下我有模型文件如图4,请问该问题该如何解决呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置