我在 CI 中路由时遇到问题。
我有一个控制器,其参数支持类别和子类别
category$1/subcategory$2
我想出了这样的条目,但它们并不完全有效。
$route['(:any)'] = 'ads/cat/$1';
$route['(:any)/(:any)'] = 'ads/cat/$1/$2';
$route['(:any)/(:any)/(:num)'] = 'ads/cat/$1/$2/$3';
一切都有效,直到我添加分页 -$3
.
我的意思是分页仅适用于category/subcategory/10...
但是当我打开它时category/10
它将 10 视为第二个参数,而不是分页......
如何让它工作?
这是我的控制器的代码:
public function cat( $cat_alias, $subcat_alias = null)
{
$this->load->library('pagination');
$limit = 10 ;
if (!empty($subcat_alias)) {
$start = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
}
else
{
$start = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
}
if (isset($_GET['city']))
{
$city = $_GET['city'];
}
$where = array( 'catalogue' => $cat_alias );
$ad_category = $this->Site_model->get_single( 'categories' , $where );
$children = $this->Site_model->get_childrens($ad_category->id);
foreach ($children as $child)
{
$childs[] = $child->id;
}
if ( !empty( $ad_category ) )
{
if (!empty($subcat_alias))
{
$where = array( 'catalogue' => $subcat_alias );
$ad_category2 = $this->Site_model->get_single( 'categories' , $where );
$data['meta']['title'] = $ad_category2->title;
$data['meta']['desc'] = $ad_category2->description;
$where = array('category_id' => $ad_category2->id);
}
else
{
$where = array( 'category_id' => $childs );
$data['meta']['title'] = $ad_category->title;
$data['meta']['desc'] = $ad_category->description;
}
$ads = $this->Site_model->get( 'ads' , $where, $limit, $start );
$data['counter'] = $this->Site_model->get_count( 'ads' , $where);
if ( !empty( $ads ) )
{
foreach ( $ads as $ad )
{
$ad_ids[] = $ad->id;
}
$where = array( 'id' => $ad_ids );
if (!empty($city))
{
$wher = array( 'slug' => $city );
$ad_city = $this->Site_model->get_single( 'cities' , $wher );
$where_filter['city_id'] = $ad_city->id;
$data['ads'] = $this->Site_model->filter('ads', $where, $where_filter, $limit, $start);
}
else
{
$data['ads'] = $this->Site_model->get( 'ads' , $where, $limit, $start );
}
if (!empty($subcat_alias)) {
$config['base_url'] = site_url(''.$cat_alias.'/'.$subcat_alias.'');
$config['uri_segment'] = 3;
}
else
{
$config['base_url'] = site_url(''.$cat_alias.'/');
$config['uri_segment'] = 2;
}
$config['total_rows'] = $data['counter'];
$config['use_page_numbers'] = TRUE;
$config['per_page'] = 10;
$config['full_tag_open'] = '<div class="pagination"><ul>';
$config['full_tag_close'] = '</ul></div>';
$config['first_link'] = false;
$config['last_link'] = false;
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = '←';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '→';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['categories'] = $this->Site_model->get_categories();
$data['current'] = $this->uri->segment(1);
// $data['meta']['title'] = $ad_category->title;
// $data['meta']['desc'] = $ad_category->description;
$this->load->view( 'cat' , $data );
}
else
{
$this->session->set_flashdata( 'alert' , 'Aktualnie brak ogłoszeń w tej kategorii.' );
redirect( '/' );
}
}
else
{
$this->session->set_flashdata( 'alert' , 'Niepoprawna nazwa kategorii.' );
redirect( '/' );
}
}
我猜你的控制器看起来像这样:
function cat($category, $subcategory, $pagination){...}
您需要对其进行一些更改,以便它可以检测第二个参数是否是分页,如下所示:
function cat($category, $subcategory, $pagination=null)
{
if($pagination == null && is_numeric($subcategory)) $pagination = $subcategory;
//your code
}
如果$category & $subcategory
是字符串,这将起作用,但如果它们是数字 (id(,则需要更多检查