R
R
Relike2015-04-18 14:00:23
Blogs
Relike, 2015-04-18 14:00:23

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

2 answer(s)
D
dmitriy, 2015-04-18
@Relike

//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;
    }
}

I
index0h, 2015-04-18
@index0h

Oh, not this shit again!
Do not write your bike, it will be worse than the rest anyway.
Look at silex router, lumen, laravel, symphonye, ​​or any other well-known framework.
Z.Y. CodeIgniter is the past, forget it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question