3

I am among less comfortable and struggle with pset7. I will be grateful for your help and explanation on the following:

Looking at View Source Page of https://ide50-name.cs50.io/register.php as well as other pages I see that register_form.php code is attached between the code of header.php and footer.php, all of which can be found in Views folder. But I don't understand how this trick works.

There is a function render() in helpers.php, and this function displays contents of register_form.php between the header and footer.

/**
     * Renders view, passing in values.
     */
    function render($view, $values = [])
    {
        // if view exists, render it
        if (file_exists("../views/{$view}"))
        {
            // extract variables into local scope
            extract($values);

            // render view (between header and footer)
            require("../views/header.php");
            require("../views/{$view}");
            require("../views/footer.php");
            exit;
        }

        // else err
        else
        {
            trigger_error("Invalid view: {$view}", E_USER_ERROR);
        }
    }

1) I am not sure I understand what extract() function does in render(). According to this page, extract() function creates local variables out of variables, which comprise an array. http://www.w3schools.com/php/func_array_extract.asp

But register_form.php (as well as login_form.php) contain html code and doesn't contain any arrays. So what local variables are being created by extract() in render(), how does extract() work with these html files?

2) Register.php (as well as login.php) call the render() function only in case the method is GET; but if the method is POST, the render() function is not called, but the https://ide50-name.cs50.io/register.php page's View source code does show register_form.php code between header.php and footer.php. How does that happen? I will be truly grateful for your help! It is truly difficult for me. Thank you very much!

3

1) Take a look at this line of code from register.php: render("register_form.php", ["title" => "Register"]);.

This is in the public folder, and it is calling render() on a file that lives within views. When doing so, it passes the array as local variables to header.php. In this case, it takes ["title" => "Register"] and effectively turns it into $title = "Register". The code extracts this value before calling require, which makes it accessible to header.php, in which you will see this code:

<?php if (isset($title)): ?>
    <title>C$50 Finance: <?= htmlspecialchars($title) ?></title>
<?php else: ?>
    <title>C$50 Finance</title>
<?php endif ?>

Now, you should be able to see that $title in line 2 of that code gets replace with the title you pass in via render().

2) When the method is POST, the page has been accessed via submission of the registration form. After checking to make sure the data submitted is valid (a requirement of the pset), you will redirect the user after logging them in (ostensibly to index.php). I don't recall what the distribution code contained and what you have to implement, but the general idea is that the register page either gives you a registration form to complete or logs you in after registration succeeds otherwise it sends you to an apology page. Does this make sense? It might help to watch the 6 or so short videos in the specs that explain the MVC paradigm.

Great questions - I applaud your effort to understand this pset in detail. It took me a little bit to wrap my mind around MVC, but once I did, it made sense as an elegant form of secure web design.

| improve this answer | |
  • Peter, Thank you very much. I don't see how header.php & footer.php are attached to other files: both header.php and footer.php codes "create" a view of every page in cs50 finance, but they are called only by render(). I don't understand how register form appears right below CS50 Finance title and below the line Quote Buy etc, i.e. right below the header.php, if the only time register.php calls the render() function is when the request method is "get"; in case of "post", render () is not called, hence I don't see how header.php and footer.php are attached. I hope my questions are clear. – Vitale Jul 30 '16 at 10:55
  • Yes, your question makes sense. Let's say that register.php calls redirect() in the case of "post" after a user has successfully registered. So, redirect() sends the user to index.php, which itself calls render() for the proper header and footer to appear. There are two options here: call render() within the code for that page or call redirect() to a page which which call render() in the context of its own code. – Peter Jul 30 '16 at 16:01
  • Thank you very much for your answer. Sorry for not replying earlier - I had to take some time to figure out these issues ) I see now the difference between if ($_SERVER["REQUEST_METHOD"] == "GET") and if ($_SERVER["REQUEST_METHOD"] == "POST"); it took me a while to understand how these two differ, and why the first one calls render() while the second doesn't - in the latter case the user is already at the page with required header and footer, i.e. at the page where he/she has to submit the information; while in the first case the user is being redirected. Thank you! – Vitale Aug 1 '16 at 17:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .