Welcome Guest, Not a member yet? Register   Sign In
Organizing Controllers into Sub-Directories
#1

I'm looking to organize controllers into sub-directories per instructions at http://www.codeigniter.com/userguide3/ge...irectories

However, it doesn't work and I'm not sure what's missing.

Going to http://www.foo.com/members goes to the member login page as expected.
Going to http://www.foo.com/members/summary gives a 404 error. Putting the logic for summary inside of controllers/Members.php works but there will be many more files and related logic so it's better if I can organize into sub-directories.

Folder structure (simplified):
Code:
-www
--application
---controllers
----members
-----Summary.php (should show member summary info... not working)
----Members.php (contains login form and logic... works)
--views
---members
----login.php (called from Members.php controller... works)
----summary.php (should be called from members/Summary.php controller... not working)
--assets
--system
--.htaccess

.htaccess file:
Code:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

What is the best to adjust to get this working? the .htaccess file? the routes.php file? readjust the folder structure? something else? Thank you in advance!
Reply
#2

You may be able to get this to work with a creative routes.php, but the main problem is probably the fact that you are trying to use a controller and directory with the same name (as far as routing is concerned) at the same level.

In other words, when you go to /members/summary, it looks for a method in your Members controller named summary. If you did not have a Members controller, it would look in the members directory for the Summary controller. As far as I can tell, it will not look for the directory as long as the controller of the same name exists.
Reply
#3

I can't test it right now, but going by the instructions, I would have my directory like this.

Code:
www
----application
--------controllers
------------members
----------------Summary.php
----------------Login.php (instead of Members.php)
--------views
------------members
----------------summary.php
----------------login.php
----assets
----system
----.htaccess

In other words, rename Members.php to Login.php and move it from controllers to controllers/members.

Then your URLs would be
Code:
http://localhost/members/login
http://localhost/members/summary
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#4

(This post was last modified: 04-30-2015, 12:21 PM by eci35.)

(04-30-2015, 11:59 AM)mwhitney Wrote: You may be able to get this to work with a creative routes.php, but the main problem is probably the fact that you are trying to use a controller and directory with the same name (as far as routing is concerned) at the same level.

In other words, when you go to /members/summary, it looks for a method in your Members controller named summary. If you did not have a Members controller, it would look in the members directory for the Summary controller. As far as I can tell, it will not look for the directory as long as the controller of the same name exists.

OK. I pulled it apart and the sub-directory structure works now. The new structure is as follows:
Code:
-www
--application
---controllers
----members
-----Login.php
-----Logout.php
-----Summary.php
-----Welcome.php (default controller)
--views
---members
----login.php
----summary.php
----welcome.php
--assets
--system
--.htaccess

Thank you @mwhitney.

Edit: @RobertSF's response came through while I was composing that response. Thank you @RobertSF as well.
Reply
#5

You're welcome! I'm glad you fixed your problem.
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#6

hey fyi - i don't know about CI 3 but with CI 2 you can't have controllers in subfolders -- but this code makes it possible:

https://degreesofzero.com/article/contro...niter.html

very clear tutorial -- i use it and it works great.
Reply
#7

(This post was last modified: 05-01-2015, 02:37 PM by CroNiX.)

(05-01-2015, 12:10 PM)cartalot Wrote: hey fyi - i don't know about CI 3 but with CI 2 you can't have controllers in subfolders -- but this code makes it possible:

Sure you can: http://www.codeigniter.com/userguide2/ge...subfolders

You just can't have more than one level of subfolders.
Reply
#8

thank you yes -- i should have said this code helps with having "sub sub" folders in the controller
Reply




Theme © iAndrew 2016 - Forum software by © MyBB