Answer the question
In order to leave comments, you need to log in
How to correctly organize routing for a blog?
What is the best way to organize a blog on CodeIgniter (in principle, on other frameworks as well)? Of course, it is desirable not to spoil the URL with words like "/post/", "/article/", "/category/", etc. Are there any ideas? The structure can be: /category/post/, /category/(page number) , /category/subcategory/post/ , /category/subcategory/(page number) . Let's say so. Are there any good solutions?
From the developments: once a long time ago I solved a similar problem in this way
function index()
{
$s1 = $this->uri->segment(1, FALSE);
$s2 = $this->uri->segment(2, FALSE);
$s3 = $this->uri->segment(3, FALSE);
$s4 = $this->uri->segment(4, FALSE);
if($s1 && !$s2 && !$s3 && !$s4)
{
$this->main_cat($s1);
}
elseif($s1 && $s2 == 'page' && intval($s3))
{
$this->main_cat($s1, intval($s3));
}
elseif($s1 && $this->m_categories->isCategoryUrl($s2) && $this->m_posts->isPostUrl($s3))
{
$this->post($s3, $s1, $s2);
}
elseif($s1 && $this->m_categories->isCategoryUrl($s2))
{
if($s3 == 'page' && intval($s4))
{
$this->subcat($s2, intval($s4), $s1);
}
elseif($s3)
{
show_404();
}
else
{
$this->subcat($s2, 0, $s1);
}
}
elseif($s1 && $this->m_posts->isPostUrl($s2))
{
$this->post($s2, $s1);
}
else
{
show_404();
}
}
Answer the question
In order to leave comments, you need to log in
//config/routes.php
$route[':any/(:any)/(\d+)'] = "startup/subcategory/$1/$2";
$route[':any/(:any)/(:any)'] = "startup/subcategory_post/$1/$2";
$route['(:any)/(\d+)'] = "startup/category/$1/$2";
$route[':any/(:any)'] = "startup/category_post/$1";
//controllers/startup.php
class Startup extends CI_Controller{
public function category_post($post)
{
echo 'category_post '.$post;
}
public function category($cat,$page)
{
echo 'category '.$cat.' '.$page;
}
public function subcategory_post($subcat,$post)
{
echo 'subcategory_post '.$subcat.' '.$post;
}
public function subcategory($cat,$page)
{
echo 'subcategory '.$cat.' '.$page;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question