Part of the EllisLab Network
   
 
How to use CI with multiple folders
Posted: 05 February 2010 07:20 AM   [ Ignore ]  
Lab Assistant
RankRank
Total Posts:  286
Joined  06-18-2009

A friend made me a challenge. Access a controller inside a folder tree.

For example. I have /controllers/folder_1/folder_2/folder_3/controller.php

and I want in my browser example.com/folder_1/folder_2/folder_3/controller/function_name

is this possbile?

Profile
 
 
Posted: 05 February 2010 08:39 AM   [ Ignore ]   [ # 1 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2280
Joined  07-30-2007

Don’t use folders to alter your URL, use URI Routing.

 Signature 

Follow me on twitter here.
MichaelWales.com | MichaelWales.info

Profile
 
 
Posted: 05 February 2010 08:59 AM   [ Ignore ]   [ # 2 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1835
Joined  12-08-2009

http://codeigniter.com/user_guide/general/controllers.html

Organizing Your Controllers into Sub-folders

If you are building a large application you might find it convenient to organize your controllers into sub-folders. CodeIgniter permits you to do this.

Simply create folders within your application/controllers directory and place your controller classes within them.

...

 Signature 

@basdflasjk | BitAuth: Authentication and Role-based Permissions | Session Library Replacement


Please read the User Guide! (Upgrading from a previous version?)

Profile
 
 
Posted: 05 February 2010 02:42 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  8
Joined  07-14-2008
Michael Wales - 05 February 2010 01:39 PM

Don’t use folders to alter your URL, use URI Routing.

Agreed, I’ve use both methods but have found that routing is the easiest to use/maintain and is far more flexible.  CI even makes it easy, for the most part you won’t have to learn regular expressions if you don’t want to.

Check it out:
http://codeigniter.com/user_guide/general/routing.html

Profile
 
 
Posted: 05 February 2010 02:51 PM   [ Ignore ]   [ # 4 ]  
Lab Assistant
RankRank
Total Posts:  286
Joined  06-18-2009

So, if i really want to use multilevel folder simply is not possible right?

Profile
 
 
Posted: 05 February 2010 10:54 PM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  18
Joined  07-04-2008

Hi,

Im having a similar problem:

I have subfolders in the controllers folder. It works with a complete index.php URI however, doesnt work when I add the .htaccess file..

What I mean is:

I have -> application/controllers/admin/controller_name.php

It works when I do www.example.com/index.php/admin/controller_name,

But as soon as I add the .htaccess file,
www.example.com/admin/controller_name gives me a ‘file not found’ error.


My Aim: Have subfolders to organize my controllers.


Can someone help?

My .htaccess file: (with ref to this: http://codeigniter.com/wiki/mod_rewrite/ )

<IfModule mod_rewrite.c>
    
RewriteEngine On
    RewriteBase 
/

    
#Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    
RewriteCond %{REQUEST_URI} ^system.*
    
RewriteRule ^(.*)$ /index.php?/$1 [L]

    
#Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    
RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond 
%{REQUEST_FILENAME} !-d
    RewriteRule 
^(.*)$ index.php?/$1 [L]
</IfModule>

<
IfModule !mod_rewrite.c>
    
# If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    
ErrorDocument 404 /index.php
</IfModule
Profile
 
 
Posted: 06 February 2010 04:12 AM   [ Ignore ]   [ # 6 ]  
Lab Assistant
RankRank
Total Posts:  286
Joined  06-18-2009

1. if you don’t have an index() function in your controller, call another function from your controller see if it works
2.    RewriteCond %{REQUEST_URI} ^system.*  don’t have this in my htaccess

Profile
 
 
Posted: 06 February 2010 05:27 AM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  18
Joined  07-04-2008

1. I do have an index() function.. and strangely everything works as long as I dont put the .htaccess file and use a complete link with index.php ( www.example.com/index.php/admin/controller_name ) 
It works perfectly with these links.

2. I followed the article here : http://codeigniter.com/wiki/mod_rewrite/

Every other controller (outside the subfolder) works perfectly with both
www.example.com/index.php/controller_1

And
www.example.com/controller_1

Its just the ones with the subfolder thats causing a problem.

Profile
 
 
Posted: 09 February 2010 02:42 PM   [ Ignore ]   [ # 8 ]  
Summer Student
Total Posts:  18
Joined  07-04-2008

Bump… anyone help?

Profile
 
 
Posted: 24 February 2010 08:41 PM   [ Ignore ]   [ # 9 ]  
Summer Student
Total Posts:  18
Joined  07-04-2008

Someone please help me..

Profile
 
 
Posted: 07 March 2010 10:41 AM   [ Ignore ]   [ # 10 ]  
Grad Student
Rank
Total Posts:  39
Joined  07-12-2007
Carl_A - 06 February 2010 10:27 AM

1. I do have an index() function.. and strangely everything works as long as I dont put the .htaccess file and use a complete link with index.php ( www.example.com/index.php/admin/controller_name ) 
It works perfectly with these links.

2. I followed the article here : http://codeigniter.com/wiki/mod_rewrite/

Every other controller (outside the subfolder) works perfectly with both
www.example.com/index.php/controller_1

And
www.example.com/controller_1

Its just the ones with the subfolder thats causing a problem.

I’ve just got this working.

I can create a folder for instance - controllers/fred
Under fred I created the controller has_a_big_nose.php ( my apologies to all the Freds out there)

So a call to www.mysite.com/fred/has_a_big_nose works a treat.

I used the very same htaccess file as you have listed, so it’s not that causing the problem.

- I’m using 1.7.2,
- I’ve got my installation under public_html (to help prevent the snoopers snooping)
- used the same htaccess file as you’ve mentioned.
- I even had the $config[‘index_page’] = “index”; and it still worked. I’ve now blanked it with no change to operation.

I’m a bit puzzled as to why it’s not working for you unless something else is going on.

What I did find is that you can only go one subfolder deep which is more than adequate.

I’ve actually obscured my admin with something like controllers/furry_rabbits/admin.php
Making my admin home page accessible via - www.mysite.com/furry_rabbits/admin

If I think of anything else I may have done to get this working - I’ll let you know.
It’s been a flurry of coding tonight so I may have missed something that would help you out.

Keep at it, you’ll get there!

Cheers
Tim

 Signature 

Before you can find the answer, you need to create the problem!

—No Links—

Profile
 
 
Posted: 07 March 2010 03:09 PM   [ Ignore ]   [ # 11 ]  
Summer Student
Total Posts:  18
Joined  07-04-2008

Hi Tim,

Thankyou for replying to such an old thread. Im unfortunately still having trouble with this issue.

Considering we have both used similar folder structures, .htaccess files and config, I fail to understand why its not working for me.

Maybe theres something that I missed? Are there any specific steps I needed to take to ensure this works for me? If so please let me know so I can back-track.

Profile