I've been doing pretty good with codeigniter but now I've hit a little snag. Instead of having everything in one controller file, I want to redistribute it throughout many files inside a sub-folder. For instance:
localhost/folderA/folderB/controllerFile
This way folder B replaces the home file for the controller and all the files can be dispersed inside this new folder since there's no need to have 10 different controller functions inside a single file (makes it large you know).
In routes I added a new line to
$route['folderA/folderB/(:any)/'] = 'folderA/folderB';
I don't even think it's even necessary to add a route but it wasn't working, always getting a 404 page no matter what. Now, the very same code for the controller works perfectly well if I bring it back by a folder. This means that /folderA/folderB/controllerFile works in /folderA/controllerFile without worries.
My objective is not to resort to moving these files up a directory but to keep the structure the same as it once was. Has anyone gone mad over something like this? I'd imagine so... lol
Thanks for reading!
Edit: For clarification this is what's going on:
/application/controllers/file.php (OK)
/application/controllers/folderA/file.php (OK)
/application/controllers/folderA/folderB/file.php (This does not work)
I could do /application/controllers/folderA/file.php and have all of the functions inside of it so it works as followed:
localhost/folderA/file
localhost/folderA/file/functionb
localhost/folderA/file/functionc
But this makes the file rather unnecessarily large when the same result could be done using sub-sub directories. Makes sense?
Instead of having everything in one controller file, I want to redistribute it throughout many files inside a sub-folder. ... all the files can be dispersed inside this new folder since there's no need to have 10 different controller functions inside a single file.
You can already put functions into multiple controller files under one directory called controllers. There was never a requirement to put all functions into one controller file.
By default, all controllers are stored in the controllers directory...
/application/controllers/
Example:
/application/controllers/Users.php
/application/controllers/Auth.php
/application/controllers/Etc.php
I don't even think it's even necessary to add a route but it wasn't working, always getting a 404 page no matter what.
If you want to use subdirectories for controllers, then these subdirectories must be contained within the controllers directory and you need to follow the documentation.
/application/controllers/subfolderA/
/application/controllers/subfolderB/
As per the documentation:
If you are building a large application you might want to hierarchically organize or structure your controllers into sub-directories. CodeIgniter permits you to do this.
Simply create sub-directories under the main
application/controllers/one and place your controller classes within them.When using this feature the first segment of your URI must specify the folder.
Each of your sub-directories may contain a default controller which will be called if the URL contains only the sub-directory. Simply put a controller in there that matches the name of your
default_controlleras specified in yourapplication/config/routes.phpfile.CodeIgniter also permits you to remap your URIs using its URI Routing feature.
EDIT:
I could do
/application/controllers/folderA/file.phpand have all of the functions inside of it so it works as followed:
localhost/folderA/file
localhost/folderA/file/functionb
localhost/folderA/file/functionc
But this makes the file rather unnecessarily large when the same result could be done using sub-sub directories. Makes sense?
Why do you keep talking about a "single large file"? You can break your functions up into as many controller files as you wish without the need of any subdirectories...
/application/controllers/Users.php
/application/controllers/Auth.php
/application/controllers/Etc.php
Respectively accessed via URL as
/users/function_foo
/auth/function_bar
/etc/function_foobar
Then using routes.php, you can achieve whatever complex URL you wish...
$route['foldera/testPage/testing'] = 'users/function_foo';
However, if you want to use a subdirectory within the controllers directory...
/application/controllers/subdirectory/Controller_a.php
Accessed via URL as...
/subdirectory/controller_a/some_function
Can be re-routed as literally anything you want...
$route['foo/bar/anything/here/tester/foobar'] = 'subdirectory/controller_a/some_function';
Okay so I figured out the solution using routes. While I don't understand exactly why codeigniter doesn't allow a 3-level folder, what I ended up doing is creating a new folder inside /application/controllers/test/ and within that folder I had my test.php file.
Then I went back to the routes configuration file and added the following line:
$route['foldera/testPage/testing'] = 'test/test';
What this effectively does is that when you direct your browser to the page
http://localhost/foldera/testPage/testing
It will treat that page as found in /application/controllers/test/test.php
It's basically giving it an alias. However if you go to localhost/test/test it will have no issues with this and treat it the exact same as foldera/testPage/testing.php In order to resolve this little issue, simply add another line to the routes config file
$route['test/test'] = 'notfound'; // Your 404 page controller
Now the only way to access that folder is directly from the URL that you want. Ta-da!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With