简体   繁体   中英

Where in my MVC PHP app do I put this build function?

I'm trying to convert my PHP app into more of an MVC app. I don't have much experience with MVC and I don't fully understand some of/all of the concepts or how to do it with PHP, so I need some help understanding where a particular function goes.

This function returns some HTML depending on if the user is logged in.

public function buildLoggedInMessage() {
    if ($this->User->isLoggedIn()) {
        return ' You are logged in as <strong>'.$this->User->getUsername().'</strong> (<a href="/logout.php">logout</a>)';
    } else {
        return ' <a href="/login.php">Login</a>';
    }
}

My initial thought was to place this function in my "controller" because it asks the User model if they are logged in (which checks the database record), however it "builds" some HTML, so maybe it should be in the view. Should I move it?

I will eventually move the HTML from the function into a template, so ignore the inline HTML.

Would the function be more suitable in the view if it was like this:

public function buildLoggedInMessage() {
    if ($this->Controller->isLoggedIn()) {
        return ' You are logged in as <strong>'.$this->User->getUsername().'</strong> (<a href="/logout.php">logout</a>)';
    } else {
        return ' <a href="/login.php">Login</a>';
    }
}

and the controller asks the model if the user is logged in?

Thanks.

I think view should not contain any business logic. Views should focus on presenting stuff, so your second solution is bad practice.

More than that, since views focus on the presentation and models handle most of the business logic, controllers should do only the necessary things to link views and models, which means fetch data from model and just insert the data into the view.

so this line of code make no sense because it means you implement business logic in controller:

$this->Controller->isLoggedIn()

Now let's see your first solution.

public function buildLoggedInMessage() {
    if ($this->User->isLoggedIn()) {
        return ' You are logged in as <strong>'.$this->User->getUsername().'</strong> (<a href="/logout.php">logout</a>)';
    } else {
        return ' <a href="/login.php">Login</a>';
    }
}

This function 'return' htmls rather than 'echo' htmls. So who is calling this function? and who will 'echo' the string from this function? I would say this is not a complete controller.

In modern web MVC, there's always some kind of 'router' handle the http requests and execute some instructions related to that. Since you wanna implement MVC pattern, you need to implement that 'router' first.

For example, you can create a 'Member' class which has a 'check' method to achieve the functionality you want.

class Member{

    public function check() {
        if ($this->User->isLoggedIn()) {
            echo ' You are logged in as <strong>'.$this->User->getUsername().'</strong> (<a href="/logout.php">logout</a>)';
        } else {
            echo ' <a href="/login.php">Login</a>';
        }
    }

}

And you need to implement the router class to handle http requests like ' http://myweb.com/member/check '.

The router code would be something like this:

    $url_segments = explode('/', $_SERVER['REQUEST_URI']);
    if (count($url_segments) == 4){
        $controller_name = $url_segments[2];
        $method_name = $url_segments[3];              
    }
    else if (count($url_segments) == 3){
        $controller_name = $url_segments[2];
        $method_name = $this->default_method;
    }
    else if (count($url_segments) == 2){
        $controller_name = $this->default_controller;
        $method_name = $this->default_method;
    }

    $this->current_controller = $controller_name;
    $this->current_method = $method_name;

    require BASEPATH . '/controller/' . $controller_name . '.php';

    $class_name = ucfirst($controller_name);
    $controller = new $class_name($method_name);

    call_user_func( array( $controller, $method_name ) );  

Create a MVC framework is not an easy work. I create a simple MVC framework for educational purpose. https://github.com/howtomakeaturn/PigFramework

Check the index.php file, and you will know what I mean router and controller.

I don't think that the point of MVC is to put HTML in a controller, if I were you I'd send some data back and make an if else statement in my view based on the send data. To make good use of an MVC you first need to understand what it is or does, so I'd recommend searching for a tutorial.

put this function in the controller from where you are calling a login function after if a user authenticated then it will set the session or flash data ie $this->session->set_flashdata('success', 'you are loged in as $username'); else redirect('login');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM