1

I'm a beginner in PHP (But I have some Java EE background) I want to create a simple Blog, but I want to do it by using PHP OOP MVC and good/best practices, I'm still thinking how to do things, but I'm really stuck at the idea of how to implement a Controller, and how will it communicate with the views, I find it very different than Java EE, and I don't know where or how to start.

Let me tell you how I'm planning to make it work, at least till the "controller" part :

  • Ultimately the design will be :

DB <-> DAO <-> Service <-> Controller <-> View. But I'll make it simple for the example.

Here we have a DAO class to communicate with the DB.

class UserDAO {

    private $db;

    // something like this by injecting the database object
    public function __construct(Database $db) {
        $this->db = $db;
    }

    public function findUserById($id) {
        $req = $db->query("SELECT * FROM user WHERE id = $id");
        //.. etc
        //.. don't worry about the syntax, what matters is that we're returning the user Object that we found.
        return $user;

    }
}

The controller will be something like this :

class Controller {

    private $dao;

    public function __construct(UserDAO $dao) {
        $this->dao = $dao;
    }

    public function loadUser($id) {
        return $this->dao->findUserById($id);
    }
}

Now, let's say that I have a index.php view file, how can I show the $user infos on the page, or sending data from view to controller later if I want to save a $user, by using best practices, not just like require the views inside controller functions etc..

Thank you very much in advance!

6
  • Here is a good read, wirth an example of what you're trying to do. onlamp.com/pub/a/php/2005/11/03/mvc_controller.html Commented Dec 25, 2016 at 22:46
  • @Kray I've already seen that topic, it didn't help. Commented Dec 25, 2016 at 23:02
  • Why not use an open source PHP framework to build your blog instead of trying to create your own framework? Commented Dec 25, 2016 at 23:39
  • @TheAlpha I want to do it the hard way to learn some tricky parts about PHP. Commented Dec 25, 2016 at 23:53
  • There are lot of PHP MVC frameworks. (Laravel, Symfony & CakePHP). They have easy ways to implement the MVC System. why you don't use it? Commented Dec 26, 2016 at 2:28

1 Answer 1

1

You could use a controller function to require using an output buffer.

/**
 * Renders a view file as a PHP script.
 *
 * This method treats the view file as a PHP script and includes the file.
 * It extracts the given parameters and makes them available in the view file.
 * The method captures the output of the included view file and returns it as a string.
 *
 * This method should mainly be called by view renderer or [[renderFile()]].
 *
 * @param string $_file_ the view file.
 * @param array $_params_ the parameters (name-value pairs) that will be extracted and made available in the view file.
 * @return string the rendering result
 */
public function renderPhpFile($_file_, $_params_ = [])
{
    ob_start();
    ob_implicit_flush(false);
    extract($_params_, EXTR_OVERWRITE);
    require($_file_);
    return ob_get_clean();
}

Taken from https://github.com/yiisoft/yii2/blob/master/framework/base/View.php#L149

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.