![]() |
How does it know which page to load? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: How does it know which page to load? (/showthread.php?tid=1389) |
How does it know which page to load? - lexxtoronto - 03-05-2015 Ok, still reading the tutorial and trying to understand how the framework works Code: public function view($page = 'home') So when you go to index.php/pages/view then $title is home, which is clear, but when you go to index.php/pages/view/about then $title is About. How is it accomplished? Because here is explicitly says - function view($page = 'home') RE: How does it know which page to load? - obiron - 03-05-2015 (03-05-2015, 12:17 PM)lexxtoronto Wrote: Ok, still reading the tutorial and trying to understand how the framework works Because home is defined as a default i.e. if you just did $this->view() then it would call 'home'. When you call $this->view('about') or $myPage = 'about'; $this->view($myPage) The default value is replaced with the value you passed in. Think of $data as goodie-bag (in fact .NET MVC uses .bag in the same way). Everything you put into $data as part of the array, is delivered to the view and then unpacked back into it's own parameter so in the controller it is $data['title'] but in the view it is $title. In the controller it is $data['someArray']['keyvalue'] and in the view it is $someArray['keyvalue'] RE: How does it know which page to load? - lexxtoronto - 03-05-2015 Awesome, I get it. Because when I say index.php/pages/view/about Im basically calling $this->view('about'). Im just curious how does it know that the default value needs to be replaced? Since 'view' is taking an explicit argument $page='home' then normally it wouldnt work because about != home. I should probably study index.php lol RE: How does it know which page to load? - CroNiX - 03-05-2015 First 2 segments in a url are controller/method, respectively, unless overridden by a route. Any additional segments get passed to the method as individual parameters. if the url was: index.php/pages/view (pages=controller, view=method), there are no additional parameters sent, so it uses the default which is hardcoded to 'home' here: public function view($page = 'home') if the url was: index.php/pages/view/products (pages=controller, view=method, products=first parameter) then $page becomes "products" here because the parameter was sent: public function view($page = 'home') RE: How does it know which page to load? - RobertSF - 03-05-2015 (03-05-2015, 01:08 PM)lexxtoronto Wrote: Im just curious how does it know that the default value needs to be replaced? Since 'view' is taking an explicit argument $page='home' then normally it wouldnt work because about != home. Well, 'view' is not really taking an explicit argument $page = 'home.' When you have a value assigned to a parameter in a function's declaration in PHP, that value is only assigned if the function is called with no argument. That 'home' is there only in case your call doesn't include a value to use instead. That's actually PHP and not Codeigniter. http://www.tuxradar.com/practicalphp/4/15/5 RE: How does it know which page to load? - lexxtoronto - 03-06-2015 Ah ok, makes sense, thank you. Im actually using Codelobster, but I never used its debugging functions. Will look into that. RE: How does it know which page to load? - RobertSF - 03-06-2015 (03-06-2015, 01:43 PM)lexxtoronto Wrote: Im actually using Codelobster, but I never used its debugging functions. Will look into that. Yes, do. I hope I can help if you have any questions. ![]() |