![]() |
Where to start? - 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: Where to start? (/showthread.php?tid=17171) Pages:
1
2
|
Where to start? - El Forum - 03-27-2009 [eluser]taro uma[/eluser] I am new to codeigniter, first look at OOP, MVC, framework, etc (unless you consider jquery a framework). I am familiar with PHP procedural style, and am a little lost on where to start getting familiar with codeigniter other than the docs. For instance, I want to include a header and footer. So rather than include files for a header, would I create a function that handles this, and put it in the library folder, calling it header.php? Then say I have in the application folder a homepage.php, in that class I would load the header.php, so that function does it's thing. Then, for the markup, do you also do the same thing? That is where I get lost on what the best way to do the next step. Say I have in the views a homepage.php file, how would I add the header.php to this? I am familiar with templates but the ones I have use were plain PHP or Smarty, which basically let you include files like PHP. In codeigniter I am missing how this to do this. Where to start? - El Forum - 03-27-2009 [eluser]taro uma[/eluser] Okay, I see this: Code: <?php Which I think I understand. But say I had some data that went in the header as well. And say that data came from a function in header.php in the library folder. So I would load the library file and that function would return whatever data I needed to the Controller? Where to start? - El Forum - 03-27-2009 [eluser]Fero[/eluser] yes, and controller would send it to header ![]() Where to start? - El Forum - 03-27-2009 [eluser]jedd[/eluser] Hi taro uma, and welcome to the forums. [url="http://ellislab.com/forums/viewthread/109698/#553141"]Here's a post I made recently[/url] that kind of answers a part of your question, I think. Have a read, anyway. It's but one way of doing this. How you handle your views is a much bigger question. I tend to create sub-directories that correlate to Controllers of mine - and put views specific to those controllers into those directories. These tend to be views that generate a portion of a page. My entire page is generated, the vast majority of the time, by a very basic view file - that pretty much does the boring preamble (html, title, head, body-open) tags - and then just includes a bunch of other data (in turn generated by mini-view files) at the right place. Note, I don't use the templating library (any of them) so I'm not sure how compatible this approach is with those. Where to start? - El Forum - 03-30-2009 [eluser]taro uma[/eluser] Thanks! regarding the views, I have a question about this also. Say I have a view that contains my sitewide navigation, and it has some dynamic parts to it. If I load it inside another view (this seems like the closest thing to templates I can think of) like inside my Frontpage.view I have this: Code: <div><?php $this->load->view('globalnav')?></div> if I need to get variables from the controller into that globalnav view, how would I do that? would I just pass them along in the Fronpage controller and do this? say I had in my controller an array called $data['navarray'] and I pass this along with other variables to my Frontpage view like this: Code: $this->load->view('frontpage',$data) So if I needed to use that navarray in my globalnav view, would I do it like this inside the frontpage view? Code: <div><?php $this->load->view('globalnav',$navarray) ?></div> Where to start? - El Forum - 03-30-2009 [eluser]jedd[/eluser] Hmm .. not sure (though I think you need to stick it as a sub-array component again). But it's one of the reasons I don't load views within other views. The other reason is that it makes it harder (to my mind) to track what's being constructed (view-wise) where. $this->load->view() - takes a third parameter, a BOOL, that if set to TRUE will return the output of itself - so you can assign that to a var within your controller, construct multiple 'view-ettes' this way, and then bang them all out in your $data[] array to your 'final' or all-encompassing view. I'd suggest this is probably easier, anyway, until you get a better handle on the shuffling around of data between views, say. Others here prefer the view-calls-another-view approach, so it's six of one stuff I expect. Anyway, yes, I'd suggest you go read about load->view, and try that approach with your view components, and pumping them back into your frontpage.php that way. In my views/ directory I create sub-directories that correspond to Controllers - and also have a 'common' sub-directory there for things like nav bars that get generated by more than one controller. Where to start? - El Forum - 03-30-2009 [eluser]taro uma[/eluser] so i would do it in the controller like this? Code: $data['globalnav'] = $this->load->view('globalnav','',TRUE); And if I needed to give that view some variable, Code: $data['globalnav'] = $this->load->view('globalnav','$data',TRUE); and it would automagically get the variables it needs from $data, assuming they are already in $data? Where to start? - El Forum - 03-30-2009 [eluser]jedd[/eluser] Correct. Your controller would then pass $data to your 'big' view: Code: $this->load->view('frontpage' , $data); views / frontpage.php would use whatever other bits in the $data array it needed, but, as I mentioned before, in my instance it's pretty much just four bits of data (all the results of view calls, as per your last post). One section of your frontpage.php view would probably look like this: Code: <div id="global_nav"> Does this approach make sense? Where to start? - El Forum - 03-30-2009 [eluser]taro uma[/eluser] Yes, but looking at your post about extending the Controller with your My_Controller, it makes sense to do this too, how does that work? Do you just make a My_Controller that extends Controller, and put your app-wide functions like navigation in there, and you extend that with other controllers? Does that mean all the functions of Controller and My_Controller are available? Can you call a model from a function inside this My_Controller if needed? Where to start? - El Forum - 03-30-2009 [eluser]jedd[/eluser] [quote author="taro uma" date="1238471032"]Yes, but looking at your post about extending the Controller with your My_Controller, it makes sense to do this too, how does that work?[/quote] Have a read of the CI manual - the section on [url="http://ellislab.com/codeigniter/user-guide/general/core_classes.html"]Extending Core classes[/url] But, yes, app-wide functions go in there. Or rather, all-controller-wide-functions. Quote:Can you call a model from a function inside this My_Controller if needed? I've not had to do this before, but I just loaded up a model from within MY_Controller, and ran a method from it - works a treat. |