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

报告相同问题?

悬赏问题

  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用