Consideration help, which way is the fastest to load views? |
[eluser]asppp[/eluser]
Yo waddup CI's I have a project at my school which is about developing a site to a local clothes shop. I chose CodeIgniter as framework and I can already say that I like it very much. I'm not great with php, and I therefor need your help. The thing I can't figure out is how I should load different pages on my site. When I built other sites without a framework, I used to build a main site, index.php which always was the same, and then I put something like: Code: <?php where i wanted my content to go, to generate different pages on my site, and the links would say: index.php?id=blog for example, or what ever I named that particular site to. But this code does not seem to work in CI, or maybe it does but I don't think it fits with all the controllers, methods et cetera. So, I looked up some alternatives for this code, and I think I would either load the "index" view on every page, or use the Template-library. I have two questions: What would be most convenient to use? and Let's say that I use the first method that I described, wouldn't it take extra time for the server to load the index-view every time? I'm confused ![]() Oh one more thing, is the Template-library the same as my first method I described? Regards, asppp from Sweden.
[eluser]dudeami0[/eluser]
Well how codeigniter handles page requests is similar to your id method. A basic url for codeigniter is: example.com/blog/view compared to your example.com/index.php?id=blog So the ID is the name of the controller, which is the first part of the url. The second is the method to call in the controller, or basically the specific page. So lets say I wanted my blog to handle viewing and searching. The URLs would be example.com/blog/view example.com/blog/search The controller would be named blog.php and placed in system/application/controllers. The PHP file would look like: Code: class Blog extends Controller { Also, if you noticed the $id and $query variables, those are grabbed from the 3rd section of the URL, so example.com/blog/search/Cats would put the value "Cats" into the $query variable. It might also be noted that without setting up a .htaccess file (Or similar, I've only used apache :S) that it will be: example.com/index.php?/blog/view Check out the tutorials and documentation. http://codeigniter.com/tutorials/ - Tutorials http://ellislab.com/codeigniter/user-guide/ - User Guide These resources helped me alot when getting used to CodeIgniter with no framework experience :p Hope this helps you out! Good luck with your project, and feel free to ask more questions ;)
[eluser]asppp[/eluser]
Hi and thanks for the reply =) Yesterday I finally understood all this that you were talking about, but thanks anyways =) It's just one thing that is a little fuzzy. And my question is like my title on the thread. In my project I now use a kind of template (the same method as Jeffrey Way uses in his video tutorial. It's based on making a template(view) that loads the header, footer and main content. Then you load the template in every method but the main content is a variable and can be changed for each method. But yet again, the header and footer is still loaded every time yes? Except that you don't need to write it out in every method, you just load the template. Won't that take a lot of time for the server to load? Should I consider using MY_controller to set up a master view? I want as little server load as possible =) thanks!
[eluser]techgnome[/eluser]
Here's how I did mine. In each function in my controller, I set a variable to the name of the view to ultimately load: Code: $data['content_view'] = 'browse/albums'; Then after I've loaded my data from the model and any other helpers I need... I load the view. In this case, I'm firing off the template. Code: $this->load->view('template/template', $data); This is what my template file looks like: Code: <?php if ($content_view == '') {$content_view = 'site/mainpage';} ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> That is the template in its entirety... it loads the header, loads a section called the album_blocks, the left nav, the main content (which is defined by the content_view I set back in the controller) ... then optionally loads the right col (based on the show_right_col I set in the controller as well) then loads my footer. I then have all my little mini views that occupy only the designated main content section of my template. -tg
[eluser]asppp[/eluser]
Hi techgnome, and thanks for the reply. I use the same method as you do with the main template and all, but thanks for the right column thing, pretty useful ![]() But doesn't it take extra time to load the template every time in every function/method? Isn't that like making a static website with the same header and footer on every page? I mean, very much code that loads every time without cause... or have I got it all wrong?
[eluser]dudeami0[/eluser]
Oh, I get your question now, sorry about that ![]() To answer the speed related question, the amount of time it takes is very small. Check out the profiling class or the caching class to speed up your site. But basically I have never had a problem with loading the header and footer in every method (Its just like including a header and footer in a basic php script). Hope this helps answer your question ![]()
[eluser]pbreit[/eluser]
asppp, if performance ever becomes an issue, you can optimize. For now, I would recommend going with the suggestions here and with the Code Igniter documentation. Some more approaches: http://codeigniter.com/wiki/Header_and_f...very_page/
[eluser]asppp[/eluser]
Thanks guys ![]() I will continue using this method, otherwise I will do as pbreit said. thanks again
[eluser]Jondolar[/eluser]
Just a quick note. Don't ever include a file who's name was pulled from a user entered value unless you are very, very careful about cleaning it. In your original example, someone could easily make your $id variable point to a file in a different directory on the same server or even on a different server, thus running code that you did not write. |
Welcome Guest, Not a member yet? Register Sign In |