views:

56

answers:

5

Say I have a controller, "Articles" but I want it to appear as a sub-folder (e.g. "blog/articles"), I can add a route like this:

$route['blog/articles'] = 'articles';
$route['blog/articles/(:any)'] = 'articles/$1';

This works fine, the only problem now is that example.com/articles and example.com/blog/articles both use the Blog controller and thus resolve to the same content. Is there a way to prevent this?

To add a little more clarity in case people aren't understanding:

  • In this example, I don't have a 'blog' controller, but I want 'articles' etc to appear to be in that subfolder (it's an organization thing).
  • I could have a blog controller with an 'articles' function, but I'm likely to have a bunch of 'subcontrollers' and want to separate the functionality (otherwise I could end up with 30+ functions for separate entities in the blog controller).
  • I want example.com/articles to return a 404 since that is not the correct URL, example.com/blog/articles is.
A: 

Routing there does not mean it will use a different controller, it just creates alias url segment to same controller. The way will be to create another controller if you are looking to use a different controller for those url segments.

Sarfraz
Not quite sure what you mean. I know it will use the same controller (articles) when I have routed it differently. And whatever controll I create I'd still have the same problem - `/mycontroller` will always go to that controller even if I'm using an alternate route for it.
DisgruntledGoat
"/mycontroller will always go to that controller even if I'm using an alternate route for it" - Don't think this is true. An entry in routes would intercept the normal flow.
stef
+1  A: 

Shove this in your controller:

function __construct()
{
    parent::Controller();

    $this->uri->uri_segment(1) == 'blog' OR show_404();
}
Phil Sturgeon
A: 

If both /blog/ and /articles/ use the same controller, you can reroute one of them to a different one by just adding a new rule in your routes file.

stef
I'm not using a blog controller, I just want it to appear as a folder. Also, I don't think just rerouting them will work, I am already rerouting but `/articles` still calls the articles controller.
DisgruntledGoat
+1  A: 

If, for some reason, you're set on routing instead of accessing the controllers in folders (you want to have a blog controller, for example, and don't want to route to it), you can do as suggested above and add the test for 'blog' to the constructor.

If you're in PHP5, you can use the constructor function like this:

function __construct()
{
    parent::Controller();
    $this->uri->uri_segment(1) == 'blog' OR redirect('/blog/articles');
}

or, in PHP4, like this:

function Articles()
{
    parent::Controller();
    $this->uri->uri_segment(1) == 'blog' OR redirect('/blog/articles');
}

I would suggest using redirect('blog/articles') instead of show_404(), though, so that you're directing users who hit /articles to the correct location, instead of just showing them a 404 page.

ebynum
+2  A: 

You can use subfolders in Codeigniter controllers, so in CI, the following directory structure works: application/controllers/blog/articles.php and is then accessed at http://example.com/blog/articles/*.

ebynum