Welcome Guest, Not a member yet? Register   Sign In
How does it know which page to load?
#1

Ok, still reading the tutorial and trying to understand how the framework works

Code:
public function view($page = 'home')
{

    if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php'))
    {
        // Whoops, we don't have a page for that!
        show_404();
    }

    $data['title'] = ucfirst($page); // Capitalize the first letter

    $this->load->view('templates/header', $data);
    $this->load->view('pages/'.$page, $data);
    $this->load->view('templates/footer', $data);

}

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')
Reply
#2

(03-05-2015, 12:17 PM)lexxtoronto Wrote: 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')

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']
Reply
#3

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
Reply
#4

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')
Reply
#5

(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
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#6

Ah ok, makes sense, thank you. Im actually using Codelobster, but I never used its debugging functions. Will look into that.
Reply
#7

(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.  Smile
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB