Welcome Guest, Not a member yet? Register   Sign In
Sub-folders or not?
#1

[eluser]Edemilson Lima[/eluser]
CodeIgniter allow us to use sub-folders for our controllers and our views. I started my application using them, but I noticed that it changes the way we organize the application scripts.

For example, without sub-folders, you can have:
Code:
// application/controllers/login.php:

class Login extends Controller {

  function Login() {
    parent:controller();
  }

  function index() {
    // shows the login form
  }

  function check($par1,$par2) {
    // check user login and password
  }

  function username($par1,$par2) {
    // search for the username, if the user forgot it
  }

  function password($par1,$par2) {
    // send the password to the user
  }
}
With the code above, you can access the controller functions with the following URIs:

/login/check/par1/par2
/login/username/par1/par2
/login/password/par1/par2

But, if you use sub-folders, you will have:
Code:
// application/controllers/login/welcome.php:
class Welcome extends Controller {

  function Welcome() {
    parent:controller();
  }

  function index() {
    // shows the login form
  }
}

// application/controllers/login/check.php:
class Check extends Controller {

  function Check() {
    parent:controller();
  }

  function index($par1,$par2) {
    // check user login and password
  }
}

// application/controllers/login/username.php:
class Username extends Controller {

  function Username() {
    parent:controller();
  }

  function index($par1,$par2) {
    // search the username, if the user forgot it
  }
}

// application/controllers/login/password.php:
class Password extends Controller {

  function Password() {
    parent:controller();
  }

  function index($par1,$par2) {
    // send the password to the user
  }
}

As you can see, without sub-folders you put the tasks of your application into the same controller, as methods of it.

With sub-folders, you must separate the code from these methods into different controller files. Well, at least if you want to access them by the same URIs I posted above.

Of course, we can have more functions inside each file. Another option would be put everything into the welcome.php file, but I think this make the sub-folders unnecessary.

My question is: did you people that already finished some works with CI, did you needed to organize the controllers into sub-folders or is it better to put everything into the controller's root directory anyway? Does it depends on the size of the application?

I hope I made myself clear. If you don't understood, I will try explain it better.
#2

[eluser]xwero[/eluser]
I think it's more about preference then about good practice. Don't go to extremes, like your example, and do it the way you feel most comfortable with.
#3

[eluser]mdavis1982[/eluser]
Can you not use the routing facilities of CI to route everything to the right place?

Cheers...

Matt
#4

[eluser]Edemilson Lima[/eluser]
Well, I noticed that when I use sub-folders, every task in my application become a separated little file. So, instead of having some big PHP files with a lot of functions on them, I will have some folders with a lot of small PHP files. May doing this way is not so bad, because CI will load only the tiniest fraction of code that is needed at each request.
#5

[eluser]xwero[/eluser]
You should benchmark it to be sure but i think i doesn't matter that much.




Theme © iAndrew 2016 - Forum software by © MyBB