Welcome Guest, Not a member yet? Register   Sign In
Building a CMS: Best Way To Handle Categories and Page Names In URL?
#1

[eluser]Vheissu[/eluser]
I am developing a CMS in Codeigniter one issue I am having is the URL structure. Because I have a pages module with a pages controller inside of it and using Modular Extensions I am routing every URL call except for the admin URL, etc and can't get the structure to turn out right.

Lets say I have categories and and a category can have a sub category and a page can belong to either a category, a sub category or doesn't belong to one at all, how would I handle that?

To put it simply, what is the best way to replicate the URL structure of Wordpress? So http://www.testsite.com/category/sub-category/page-name or http://www.testsite.com/category/page-name or http://www.testsite.com/page-name

I am storing the page slug separated as hyphens in the database, so I wonder if a function can be written that takes the URL and finds it in the database or something? Perhaps if you've developed something similar would you mind sharing how you did it please?
#2

[eluser]natefons[/eluser]
im no CI guru, but the first thing that came to my mind with:
http://www.testsite.com/category/sub-category/page-name
is to have a your controller take arguments

function foo($cat='',$subcat='',$page_name='')
that way, you can pass either 0 or 3 arguments like your url has.
#3

[eluser]Vheissu[/eluser]
Hmm, that is a very good idea. In-fact, it may just work. Sometimes the simplest solutions are the best ones. I am using a remap function to capture all requests to my pages controller, but I'm sure I could somehow modify my controller to work that way.
#4

[eluser]Vheissu[/eluser]
I did some thinking and this couldn't possible work, unless I have blank slashes in my URL. By your approach I would always have to have 3 parameters in my URL.

So if I have only 2 parameters in my url it would be /category/page-name my function would take the first argument as a category and the second as a sub category instead of the page name. Anyone else? I noticed our in-house CMS at work that is running on top of zend stores the router URL's in the database like /about-us/board-of-directors anyway I can do something like that?

I haven't had much experience with custom URL's in Codeigniter yet and it is my weakness. Thanks for your help guys.
#5

[eluser]Phil Sturgeon[/eluser]
Code:
$segments =& func_get_args();
    switch (count($segments))
    {
        case 3:
            list($cat, $subcat, $page_name) = $segments;
        break;
    
        case 2:
            list($cat, $page_name) = $segments;
            $subcat = NULL;
        break;
    
        default:
            list($page_name) = $segments;
            $cat = $subcat = NULL;
        break;
    }
#6

[eluser]Vheissu[/eluser]
Awesome; thanks Phil. I haven't had the chance to try that, but looks like an implementation that will work for my needs. This is better than the idea I came up with last night that basically was 3 routes and then 3 separate functions, one for a page without a category, one with a category and one with a category and sub category.

Will that code work on a _remap function Phil? Or do I just create a new function in my controller instead with the arguments defined.
#7

[eluser]Unknown[/eluser]
How can I use this case switch in:

function categories($cat='',$subcat='',$page_name=''){
}
?

suppose in many of my cats and pages, url is: site.com/news/europe/economics_in_italy
or site.com/news/europe
#8

[eluser]wiredesignz[/eluser]
[quote author="Phil Sturgeon" date="1287074949"]
Code:
$segments =& func_get_args();
    switch (count($segments))
    {
        case 3:
            list($cat, $subcat, $page_name) = $segments;
        break;
    
        case 2:
            list($cat, $page_name) = $segments;
            $subcat = NULL;
        break;
    
        default:
            list($page_name) = $segments;
            $cat = $subcat = NULL;
        break;
    }
[/quote]
@phil, How about this instead?
Code:
list($cat, $subcat, $page_name) = array_pad($segments, 3, NULL);
#9

[eluser]Phil Sturgeon[/eluser]
@Wired: When I saw that snippet I hoped I just overlooked something, but that does not do the same job.

Code:
echo "<pre>";
    $segments = array('one', 'two');
        switch (count($segments))
        {
            case 3:
                list($cat, $subcat, $page_name) = $segments;
            break;

            case 2:
                list($cat, $page_name) = $segments;
                $subcat = NULL;
            break;

            default:
                list($page_name) = $segments;
                $cat = $subcat = NULL;
            break;
        }

list($cat2, $subcat2, $page_name2) = array_pad($segments, 3, NULL);

var_dump($cat === $cat2, $subcat === $subcat2, $page_name === $page_name2);

If 3 segments there is a category and a page. If 3 there is a sub category too. If only 1 then there is a page.

This is not how I would implement cats and pages, but this is what the original poster wanted.
#10

[eluser]wiredesignz[/eluser]
So you really want to reverse the $segments array first pull off the $page_name and add a check for $cat == NULL and assign the $sub_cat value to $cat.

Also in your example a $segments array with 4 elements or more will assign the first value to $page_name.




Theme © iAndrew 2016 - Forum software by © MyBB