Welcome Guest, Not a member yet? Register   Sign In
[solved] Tricky Folder Layout
#1

[eluser]helmutbjorg[/eluser]
Hi Guys,

Really starting to get into codeigniter now. One more thing before i'm fully sold.

Say i want to have the following folders on my website
Code:
example.com/account
example.com/account/orders
example.com/account/images

Each of those relates to a controller
Code:
controllers/account.php
controllers/account_orders.php
controllers/account_images.php

Each controller has several functions for example
Code:
example.com/account/delete
example.com/account/orders/view
example.com/account/orders/delete
example.com/account/images/upload

But I cannot do this. The name conflict with the account controller interferes with having the orders and images controllers accessed from what is essentially a subfolder.

Even if i try the following code in routes.php config
Code:
$route['account/orders'] = 'account_orders';
$route['account/orders/(:any)'] = 'account_orders/$1';

I haven't explained it very well but do you guys get what i mean?
#2

[eluser]xwero[/eluser]
The file structure doesn't need to reflect the url structure.
If you have not a lot of code you can put everything in one controller. If you have a lot of code you can create a directory and add controllers to it.

For routing as you expect you have to put the specific route above the general route.
#3

[eluser]helmutbjorg[/eluser]
So what you're saying is this?

I want the following folders in the url
Code:
example.com/account
example.com/account/orders
example.com/account/images

Each of those relates to a controller
Code:
controllers/account_index.php
controllers/account_orders.php
controllers/account_images.php

By using the following routes?
Code:
$route['account'] = 'account_index';
$route['account/(:any)'] = 'account_index/$1';
$route['account/orders'] = 'account_orders';
$route['account/orders/(:any)'] = 'account_orders/$1';
// etc...

Is that correct?
#4

[eluser]xwero[/eluser]
Lets say you have not a lot of code and you put all the things in one controller
Code:
class Accounts extends Controller
{
    function index()
    {
       switch($this->uri->segment(2))
       {
          case 'delete': /* code */ break;
          // ...
          default: /* no extra segments */ break;
       }
    }

    function orders() // same structure for images
    {
       switch($this->uri->segment(3))
       {
          case 'delete': /* code */ break;
          // ...
          default: /* no extra segments */ break;
       }
    }
}
Then you can set up your routing as follows
Code:
$route['account/(delete|other|actions)(.*)'] = 'account/index/$1$2';
And that is the only routing you need because the account/orders will be routed to the correct method by default.

If you want to split the controllers to have smaller files you could go for following file structure

actions/actions.php : the index function in the controller above
actions/orders.php
actions/images.php

And in the routing this will have following result
Code:
$route['account'] = 'account/account';
$route['account/(delete|other|actions)(.*)'] = 'account/account/$1$2';
The cases in the switch become methods.
#5

[eluser]helmutbjorg[/eluser]
Thanks heaps xwero! So going kinda from what you have said I have come up with a working solution.

Say i want to have the following folders on my website
Code:
example.com/account
example.com/account/orders
example.com/account/images

Each of those relates to a controller
Code:
controllers/account.php
controllers/account_orders.php
controllers/account_images.php

Each controller has several functions for example
Code:
example.com/account/delete
example.com/account/orders/view
example.com/account/orders/delete
example.com/account/images/upload

Adding the following code into routes.php config
Code:
$route['account/orders/(:any)'] = 'account_orders/$1';
$route['account/orders'] = 'account_orders';
$route['account/images/(:any)'] = 'account_images/$1';
$route['account/images'] = 'account_images';

Which works great!

Now if we want to take this method further. Say you want to put another controller underneath the orders folder like so:
Code:
example.com/account/orders/archive/

Then that would map to the following controller
Code:
controllers/account_orders_archive.php

And that controller can now have its own functions
Code:
example.com/account/orders/archive/download
example.com/account/orders/archive/generate
example.com/account/orders/archive/delete

And you add the following to the top of routes.php config file
Code:
$route['account/orders/archive/(:any)'] = 'account_orders_archive/$1';
$route['account/orders/archive'] = 'account_orders_archive';
$route['account/orders/(:any)'] = 'account_orders/$1';
$route['account/orders'] = 'account_orders';
$route['account/images/(:any)'] = 'account_images/$1';
$route['account/images'] = 'account_images';
Note how the rule must be above the subfolder it is contained within. So that there are no conflicts. The higher rule takes priority. That was the key!

Thanks for your help. I like this solution as it keeps the controllers folder organised. If anybody has any better solutions for me let me know!
#6

[eluser]Phil Sturgeon[/eluser]
You can reduce your rotues a little there to make them easier to maintain and save you adding a new one for each controller you add.

Code:
$route['account/(:any)/archive/(:any)'] = 'account_$1_archive/$2';
$route['account/(:any)/archive'] = 'account_$1_archive';
$route['account/(:any)/(:any)'] = 'account_$1/$2';
$route['account/(:any)'] = 'account_$1';




Theme © iAndrew 2016 - Forum software by © MyBB