Multi-level subfolders for controllers in CI 2.x |
[eluser]sehummel[/eluser]
I've seen several posts on here that talk about how to handle multi-level subfolders in CI 2.x but no one has posted the complete code. I found this post: http://ellislab.com/forums/viewthread/129469/, but the complete solution was not posted. Could someone please post the entire script, with detailed instructions on how to implement. I couldn't sort out how to make the changes from the 1.7.1 code. Thanks. Scott
[eluser]Damien K.[/eluser]
Well, here you go sehummel: yet another multi-level subfolders controller extension. This was for 2.0.0. No changes in the affected methods in 2.0.2, so should work there as well. This will support things like the following: (Note that 'default_controller' is as defined in 'config/routers.php') If you have the following controllers: controllers/user/<default_controller>.php controllers/user/profile.php Then: http://www.myhost.com/user/ maps to http://www.myhost.com/user/<default_controller> http://www.myhost.com/user/prefs maps to http://www.myhost.com/user/<default_controller>/prefs http://www.myhost.com/user/profile maps to http://www.myhost.com/user/profile I think there is a "bug" in CI 2+ where 'show_404()' will error because all the core classes are not loaded yet, hence it was not use in this code. HTH, Damien K. Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
[eluser]sehummel[/eluser]
This is great, Damien, but I can't seem to get it working. I placed the code in a file called MY_router.php in the application/libraries folder and uploaded it. I have a controller called markets that I put in the test folder in the cms folder (controllers/cms/test/markets.php). I uploaded this folder structure and even built a route for it. But I get a 404 error. My subclass prefix is set to the default of "MY_" so that's not the problem. Any idea off hand what I might be doing wrong? Will your router file work for the folder structure I have? It looks like it from your post, but I wasn't quite sure. I really appreciate your help with this!
[eluser]cideveloper[/eluser]
MY_router.php should be put into application/core folder
[eluser]gbar[/eluser]
this solution was useful like for me, I tried for days ... thanks
[eluser]gbar[/eluser]
How could I extend your solution also to the models folder and views folder? Thanks
[eluser]Damien K.[/eluser]
@gbar I think multi-level folders/directories already works for models and views. $this->load->model('shopping/international/exchange_model'); $this->load->view('shopping/cart/favourite');
[eluser]gbar[/eluser]
I worked on this thing all day yesterday, making a lot of testing ... is true, a structure so it works: Code: $this->load->model(‘shopping/international/exchange_model’); but it is painfully slow, at least in my experience I had ... This morning I reported levels of depth views and models in a single folder Code: $this->load->model(‘folder/model’); Code: $this->load->view(‘folder/form_view’); and the application has reacquired the optimal speed, while your solution for controllers, it works really well.
[eluser]jjDeveloper[/eluser]
I came up with something nifty that works for me, feel free to try it out. In your applications core library if you don't already have one add a subclass of CI_Loader and override the view function like so. Code: class MY_Loader extends CI_Loader { To make this function work for your needs however you will have to add case statements for the sub-folders that you wish to abbreviate so to speak. Lets say in your views you have a folder named content you would just add the following code to the switch statement. Code: case 'c': The resulting view calls to this folder can now be done using the following load call Code: $this->load->view('c<>some_content'); This will also work if you have multi-level sub-folders like this Code: $this->load->view('f<>r<>some_view'); This would have the same effect as doing this. Code: $this->load->view('forms/registration/some_view'); I find it helps code readability and also results in less typing when you wish to load views out of sub-folders. Keep in mind this addition will still retain the normal functionality if you load the view with its full path as well. I just thought this might be something you may want to look at. I enjoy using it so hope someone else does too. Cheers |
Welcome Guest, Not a member yet? Register Sign In |