Link structures

#1
[eluser]Jmz[/eluser]
I've been reading up on CI and I think I have the basics, but there's one thing I can't work out about the link structure.

Say I'm making a blog where a user has an admin panel and an add page option. My class for the admin panel would look like:

Code:
Class Admin extends Controller{
    
    function Admin(){
        parent::Controller();
    }
    
    function Index(){
        //Do something    
    }

    function Add_page(){
        //Do something    
    }

}

If I go to mysite.com/admin it does whatever is in the Index function. If I go to mysite.com/admin/add_page it does whatever is in the Add_page function. Makes sense.

What if I want to change the link to mysite.com/admin/pages/add_page so that it's more organised? Can I do this?

#2
[eluser]n0xie[/eluser]
Might be easier to make a folder in your controllers direcotry named admin, and then add a page controller there with a method 'add':

Code:
application
  - controllers
    - admin
      pages.php

#3
[eluser]Jmz[/eluser]
That works! Thanks.

#4
[eluser]Jmz[/eluser]
I think I've hit a snag. If I go to mysite.com/admin I want to show an admin home page. I've tried adding an admin.php controller like:

Code:
application
  - controllers
    admin.php
    - admin
      pages.php

With the index function to show the admin panel home page but then if I go to mysite.com/admin/pages it still shows the admin home rather than the pages home.

#5
[eluser]WebsiteDuck[/eluser]
You need to get rid of admin.php and create /controllers/admin/home.php (or whatever you have your default_controller set to in /config/routes.php)

The default_controller works for any folder in your controllers folder.
/controllers/home.php is default for example.com/
/controllers/admin/home.php is default for example.com/admin/

#6
[eluser]Sean Gates[/eluser]
If you want you can set routing up from within your controller. Example:

Code:
Class Admin extends Controller{
    
    function Admin(){
        parent::Controller();
    }
    
    function index(){
        //Do something    
    }

    function add_page(){
        //Do something    
    }

    function pages($other_method = NULL){
        if(!is_null($other_method)){
            $this->$other_method();
        }
        else{
            // default for /admin/pages
        }
    }

}

Then you can use either:

Code:
http://somesite.com/admin/pages/add_page

or

Code:
http://somesite.com/admin/add_page

#7
[eluser]Sean Gates[/eluser]
BTW, my method above is fairly crude. There are other things you need to consider when doing things like this.

#8
[eluser]Jmz[/eluser]
Thanks for the replies, is there a part of the user guide or some other article I could read that would explain stuff like this? It seems the things I have read don't go into that much detail.

#9
[eluser]WebsiteDuck[/eluser]
Sure, check out Organizing Your Controllers into Sub-folders in the Controllers section of the User Guide.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.