Question

I would like to implement controllers that connect to any specific views like MVC does. I'm not using any framework that provided in PHP.

So, I need some guide and advice on doing it.

I have some controllers and views. For my views,i would like to just output my data only. My concern now is how my function (like create() ) in controllers, can get all the $_POST['params'] that users input data in my views/create.php, and create a new Model in the create() controllers's function.

So,right now, i'm thinking to do in this way, I will create MyViews class in my controllers folder. The purpose is loading the specific views and get all the $_POST params into an object. Then, every controllers like Users_controllers, will create MyViews. In the function of Users_controllers, like create(), destroy(), I might use the function in MyViews to load specific views to load the object.

I found a source that load views

<?php
class MyView {
protected $template_dir = 'templates/';
protected $vars = array();
public function __construct($template_dir = null) {
    if ($template_dir !== null) {
        // Check here whether this directory really exists
        $this->template_dir = $template_dir;
    }
}
public function render($template_file) {
    if (file_exists($this->template_dir.$template_file)) {
        include $this->template_dir.$template_file;
    } else {
        throw new Exception('no template file ' . $template_file . ' present in     directory ' . $this->template_dir);
    }
}
public function __set($name, $value) {
    $this->vars[$name] = $value;
}
public function __get($name) {
    return $this->vars[$name];
}
} ?>

hmm,I have no idea How I can detect the _POST params

    if(isset($_POST['Post']))
    {
        $model->attributes=$_POST['Post'];
        if($model->save())
            $this->redirect(array('view','id'=>$model->id));
    }

this is the Yii framework I observed. How could I detect params whether is $_POST or $_GET after load a specific views.

Any guidance and advice to archive my tasks?

Was it helpful?

Solution

Unrelared to question
You have one major problem: your ability to express what mean is extremely limited. The question, which you asked, was actually unrelated to your problem.

From what I gather, you need to detect of user made a POST or GET request. Do detect it directly you can check $_SERVER['REQUEST_METHOD'], but checking it withing controller might be quite bothersome. You will end up with a lot of controller's methods which behave differently based on request method.

Since you are not using any of popular frameworks, is would recommend for you to instead delegate this decision to the routing mechanism.

A pretty good way to handle this, in my opinion, is to prefix the controller's method names with the request method: postLogin(), getArticles() etc. You can find few additional example here. If there is a POST request, it will have something in $_POST array.

What are calling "views" are actually templates. If you read this article, you will notice, that the code there is actually an improved version of your MyView. Views are not templates. Views are instances which contain presentation logic and manipulate multiple templates.

P.S. If you are exploring MVC and MVC-inspired patterns in relation to PHP, you might find this post useful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top