2013-02-07 77 views
0

我想分頁我的索引http:localhost/mysite/home但如果我只寫這個http:localhost/mysite/home頁面說:「page not found」,但如果我寫http :localhost/mysite/home/3它的工作!如何我可以配置我的路由獲取空參數?我試着用(:任何)和(NUM),但無法正常工作Codeigniter路由如何允許null參數

我的路線文件是:

 $route['404_override'] = 'welcome/no_found'; 
     $route['home/'] = "welcome/home/$1"; 

我的控制器:

 $config['base_url'] = base_url().'/mysite/home'; 
     $config['total_rows'] = $this->mtestmodel->countNews(); 
     $config['per_page'] = 2; 
     $config['num_links'] = 20; 
     $this->pagination->initialize($config); 
     $data['paginator']=$this->pagination->create_links(); 
     $data['news']=$this->mtestmodel->show_news($config['per_page'],intval($to)); 

// $到在URL BASE_URL參數()/ mysite的/家用/ 3

回答

1

route.php文件更改爲以下:

$route['404_override'] = 'welcome/no_found'; 
$route['home/(:num)'] = "welcome/home/$1"; 
$route['home']   = "welcome/home"; 

這樣,您的應用程序就會捕獲這兩個請求(http://localhost/mysite/homehttp://localhost/mysite/home/3),並將它們發送到您的控制器。

然後,您應該可以訪問您的$to變量(該變量已作爲第一個參數傳遞到控制器的函數中),或者使用$this->uri->segment(2)來代替。

例如

public function home($to = 0) { 
    $config['base_url'] = base_url().'/mysite/home'; 
    $config['total_rows'] = $this->mtestmodel->countNews(); 
    $config['per_page'] = 2; 
    $config['num_links'] = 20; 
    $this->pagination->initialize($config); 
    $data['paginator']  = $this->pagination->create_links(); 
    $data['news']   = $this->mtestmodel->show_news($config['per_page'],intval($to)); 

} 

希望有所幫助!