Welcome Guest, Not a member yet? Register   Sign In
Confused about which view method is being called in Tutorial - Static pages [Solved]
#1

[eluser]Sameh R. Labib[/eluser]
BTW I'm new to CI.

The CodeIgniter User Guide Version 2.1.0 has a tutorial called Static pages. In this tutorial a controller is created called Pages. Pages has a method called view. Here is the tricky part. The view method has a statement which calls the view method on $this. Yet I don't believe that is the view method which is really being called. The reason I believe this is because I've seen the same statement in other tutorials which don't name the Pages controller method view.

My question is as follows. I know the code works but how does it have this statement which calls view without calling itself? Furthermore, how is it calling the correct view method?

Here is the code:

Code:
<?php

class Pages extends CI_Controller {

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

   if ( ! file_exists('application/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);

}
}

The statement I'm referring to could be any of those which call the view method such as:

Code:
$this->load->view('templates/header', $data);
#2

[eluser]Aken[/eluser]
Read more of the user guide besides the tutorial Smile http://ellislab.com/codeigniter/user-gui...views.html
#3

[eluser]Sameh R. Labib[/eluser]
I thank you for your reply. However, I noticed that the UG page told me to read the controller page first. I am also under the impression that all the documentation just talks about how to make CI work; but, it doesn't explain from a PHP syntax point-of-view why it works. It just makes sense that If you call $this->method you would be calling method which belongs to $this object. If someone can clear things up for me I'd appreciate it! Thanks.
#4

[eluser]InsiteFX[/eluser]
Because it is being called from the broswer
Code:
// example
http://localhost/pages/view/home
// controller/method/pramaters
class Pages extends CI_Controller {

public function view($page = 'home')
{
#5

[eluser]Aken[/eluser]
The statement isn't calling $this->view(). It's calling $this->load->view().
#6

[eluser]InsiteFX[/eluser]
$this->load is calling the ./system/core/Loader.php Class to load the view file.
#7

[eluser]Sameh R. Labib[/eluser]
Thanks again. I just figured it out and was about to post. You are correct.

The reason for my confusion is that I thought Fluent Interfaces involved methods which return a reference to their own objects. My problem is that I'm trying to learn OOP, MVC, and CodeIgniter all at the same time. I'm really glad you had an answer for me - just in case I hadn't figured it out myself. Actually, talking about it helped too!

Thanks, I'll look up your name in the next screen.
#8

[eluser]InsiteFX[/eluser]
CodeIgniter creates itself as a Super Object!
Code:
private $CI;

public function __construct()
{
    $this->CI = get_instance();
}

You now will have access to the CodIgniter Super Object using $this->CI->!




Theme © iAndrew 2016 - Forum software by © MyBB