![]() |
Couple questions from a novice - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Couple questions from a novice (/showthread.php?tid=2523) |
Couple questions from a novice - El Forum - 08-10-2007 [eluser]Sulako[/eluser] I worked only with PHP before, and now i think it's time to switch to framework. I looked at CodeIgniter and it seems nice, but i can't figure if there is a way to do some things. So, 1) Can i call an action of one controller from another controller? Say i have some common block in most pages of site (menu, poll, hotnews and so on), so i'd like to get data for that block from appropiate controller (hotnews from news controller) just by calling appropriate action. CakePHP has something like that - 'requestAction' function. 2) Can i implement dynamic routing? That is, i'm not satisfied with http://site/controller/action/.../ model, i want to make tree-like directory structure with any node being a controller. (http://site/dir1/dir2/action) This structure should be in DB and should be easy to modify, so just the array of routes won't do the thing. Couple questions from a novice - El Forum - 08-10-2007 [eluser]richard_ctv[/eluser] Welcome to CI! [quote author="Sulako" date="1186750645"] 1) Can i call an action of one controller from another controller? Say i have some common block in most pages of site (menu, poll, hotnews and so on), so i'd like to get data for that block from appropiate controller (hotnews from news controller) just by calling appropriate action. CakePHP has something like that - 'requestAction' function. [/quote] I think for item 1 you'll want to put the date requisitions into models, and then call those from your controller. Then you can share them simply between controllers. In a current project I have a bunch of models that mostly provide data for one controller, but there are a few summary type functions that get called from other controllers. [quote author="Sulako" date="1186750645"] 2) Can i implement dynamic routing? That is, i'm not satisfied with http://site/controller/action/.../ model, i want to make tree-like directory structure with any node being a controller. (http://site/dir1/dir2/action) This structure should be in DB and should be easy to modify, so just the array of routes won't do the thing.[/quote] For item two you can do a bunch of stuff with _remap. See the docs. This allows you to take complete control of URI processing and do whatever you want with it. I suppose you could have one controller for the whole site if _really_ wanted but this would be a bit crazy... :-) For a project I am working on I have a hierarchy in the DB and this drives the URI structure of the site dynamically based on the (ever changing) data. Getting URIs out of the db and onto the front end and then getting it to all map back to what you want though can be a bit of fun. I have three projects on the go with CI at the moment, and as far as I have found there is not much you can't do. Couple questions from a novice - El Forum - 08-10-2007 [eluser]Sulako[/eluser] [quote author="richard_ctv" date="1186756256"]Welcome to CI! I think for item 1 you'll want to put the date requisitions into models, and then call those from your controller. Then you can share them simply between controllers.[/quote] So, moving business logic from controllers to models... well, may be this will do, although it's not what i was looking for. ![]() [quote author="richard_ctv" date="1186756256"]For item two you can do a bunch of stuff with _remap. See the docs. This allows you to take complete control of URI processing and do whatever you want with it. I suppose you could have one controller for the whole site if _really_ wanted but this would be a bit crazy... :-)[/quote] That returns us to question 1 ![]() I can have all url routed to one navigation controller, parsed there, decide which controller to use, but then i need to call an action of this controller... Couple questions from a novice - El Forum - 08-10-2007 [eluser]deviant[/eluser] There is a post somewhere about unlimited nesting of directories for controllers, allowing you to create a tree-like structure using directories. Would that suit your purpose or are you specifically looking to store the structure in a DB? Couple questions from a novice - El Forum - 08-10-2007 [eluser]Sulako[/eluser] From the first glance, the solution in that post ( http://ellislab.com/forums/viewthread/56100/ ) just discards anything before controller name, allowing to have imitation of directory structure. Still, there should be a controller name in url, and i think that something like http://site/about/feedback/feedbackform/ or http://site/about/staticpage/ does not look well. Also, it is just an imitation - there can be any string before controller name ( http://site/about/feedback/feedbackform == http://blablabla/feedbackform ). In fact, i need the ability for administrator to create/delete/move new pages/sections of site in a tree-like structure. Keeping this structure in DB is convenient. May be that could be done by implementing some logic into routes.php... Couple questions from a novice - El Forum - 08-10-2007 [eluser]Michael Wales[/eluser] For #1: Yes, it's PHP, you can do anything you want! ![]() For #2: Using URI Routing you can do whatever you want with your URLs. Couple questions from a novice - El Forum - 08-10-2007 [eluser]Henrik Pejer[/eluser] You can have the same routing rules in the db as you have in the route.php config file, but, you'll have to load these very early if you want to 'trick' the system. If that would help you, of course. The problem with this is that the database class (or library if you will) is not loaded. That means that you would have to connect to the database without that library. I use the hook pre_system to load my routing from the database. Then CI loads the routing-class like nothing happened. Its not really a neat solution. I'd love to have CI-settings in the database, but since CI is built for speed, and connecting to a database to read some configs probably isn't the most effective way to do it. Couple questions from a novice - El Forum - 08-10-2007 [eluser]deviant[/eluser] What you *could* do is keep a flat-file version of the file structure somewhere and have CI update it whenever the page structure is changed. The main problem with using a database like Henrik said is that it isn't loaded until the routing is finished and the Controller is instantiated. Using a flat-file would probably be fine as long as you aren't considering a large number of pages and depending on how often you see the file structure being updated. Couple questions from a novice - El Forum - 08-10-2007 [eluser]Colin Williams[/eluser] You can implement some fairly intelligent routing using the regular expression feature. I know routing feels very rigid sometimes (most frameworks can feel rigid) but you should reach a point where it works for the site. If you really can't get comfortable with the rigidity, try something more fluid like Drupal. Couple questions from a novice - El Forum - 08-11-2007 [eluser]esra[/eluser] You could rewrite the Router library to handle the needs for item 2. It would mean registering all of your modules (controllers) in a database table along with the various routes for accessing the different methods. For an example of this general approach, look at the Joomla CMS or Mambo CMS. You should be able to develop your own CI-specific approach based on your own description. |