Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8883233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:45:57+00:00 2026-06-14T20:45:57+00:00

Human readable URLs with nested categories like /category/subcategory/n-subcategories/article. I’m using CakePHP 2.2.3 and can’t

  • 0

Human readable URLs with nested categories like /category/subcategory/n-subcategories/article. I’m using CakePHP 2.2.3 and can’t find a proper solution for a routing problem. Using 2 Tables:

  • articles (could also be products or posts or…)
    • have just a normal “single” view
    • an article belongs to one category
  • categories
    • nested (Tree behaviour with n-levels)
    • one category can have many articles
    • category-view lists all articles, that are related to this category
    • category view uses paginator for showing article lists

A very common example I guess. But how do I have to define the router now, to get URL-paths with the nested categories like this:

/categoryname1                                         (showing category view)
/categoryname1/articlename1                            (showing article view)
/categoryname2/articlename2                            (showing article view)
/categoryname2/subcategoryname1                        (showing category view)
/categoryname2/subcategoryname2/articlename4           (showing article view)
/n-categoryname/././...n-subcategoryname               (showing category view)
/n-categoryname/././...n-subcategoryname/n-articlename         (article view)

I tried to make all routes fix in the routes.php, but that is not very comfortable and I think there should be a dynamic solution.

I also tried to automatically generate all routes out of category- and article-alias and save them in a separate “routes” database table – it worked, but I don’t think it’s really necessary to define hunderets of single routes?!

I also tried just to define all the categories fix in router, like

Router::connect(
  '/any-category-name',
  array('controller' => 'categories', 'action' => 'view', 1)
);

and then for the articles

Router::connect(
  '/any-category-name/:slug',
  array('controller' => 'articles', 'action' => 'view'),
  array('pass' => array('slug'))
);

But with this method, all articles are available in all categories, which isn’t a good solution. And I thought about using

Router::connect(
  '/:slug', ...

but I don’t know how to go on, because there are two different controllers and two different views possible (also I don’t know if Pagination will still work in this case and what will happen, if I also want to use more controllers/actions in the installation).

I think it shouldn’t be so difficult to get nested urls with two controllers (categories and articles) in Cake?! Thanks for any helpful advise!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:45:58+00:00Added an answer on June 14, 2026 at 8:45 pm

    I think you have to check for two things:

    1) Check the number of categories in the url and
    2) Check if the last parameter is a category or an article

    Handle both checks within a (dynamic) route may be very difficult. I would suggest to create just one route for all these requests and do the checks for 1) and 2) in a controller.

    The route may be something like this:

    Router::connect(
      '/*',
      array('controller' => 'outputs', 'action' => 'index')
    );
    

    I called the controller for this route OutputController because this will be the controller that handles the output for all these urls.

    class OutputController extends AppController
    {
      public $uses = array('Article', 'Category');
    
      public function index()
      {
        // Get n parameters from url (1)
        $args = func_get_args();
        $last_arg = $args[count($args) - 1];
    
        // Check if this is an article (2)
        $article = $this->Article->find('first', array(
          'conditions' => array('Article.slug' => $last_arg
        ));
        if (!empty($article)) {
          $this->set('article', $article);
          $this->render('article');
        }
    
        // Check if this is an category (2)
        $category = $this->Category->find('first', array(
          'conditions' => array('Category.slug' => $last_arg
        ));
        if (!empty($category)) {
          $this->set('category', $category);
          $this->render('category');
        }
    
        // Page not found
        if (empty($article) and empty($category)) {
          throw new NotFoundException();
        }
      }
    
      // ...
    

    To display an article, the view ‘Output/article.ctp’ is used. For a category, CakePHP renders ‘Output/category.ctp’. In addition you can use the parameters in $args to fetch all the necessary data for your (sub-) categories.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I like the different human readable strings you can parse with the DateJS javascript
I have multiple objects that I'd like to be accessible via human-readable URLs, so
How can I convert byte size into a human-readable format in Java? Like 1024
How can I generate human readable llvm bitcode (extension .ll) from the binary llvm
I would like to format some commands execution times in a human readable format,
How can I write human readable timestamp in linux kernel? I think do_gettimeofday returns
does anybody know whether Chrome Developer Tools can format javascripts into human readable form
how can I convert this epoch time to human readable date time 1331515367 I
js and css files can be pretty big in order to be human readable.
How can I go about getting a human readable device type from a IOBluetoothDevice

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.