useCase
useCase

Reputation: 1275

How to connect controller to view in PHP OOP?

Now i am using PHP OOP Programming, without framework, I start to create a Blog Application. First i create a Bootstrap file then all cases should be handle through this, then i create a Handler class for handle the login and post classes, right now display the value in login handler class, then how to connect into view part.

Upvotes: 0

Views: 2215

Answers (4)

preinheimer
preinheimer

Reputation: 3722

Generally you'd have a specific function or class that is told which view template to load, and it loads it. $view->loadTemplate('userHome.html'); or the like. This limits the scope of the variables accessible in the view to variables you specifically assign to it ($view->userName = 'fred';). So you'll need to make that function/class.

For example if you have a user profile view, it could look like this:

<div class='profile'>
   <img src='<?php echo $avatar; ?>'>
   <h1><?php echo $username; ?></h1>
   <table>
      <tr><th>Registration date:</th><td><?php echo $regdate; ?></td></tr>
      <tr><th>Lastlogin:</th><td><?php echo $logindate; ?></td></tr>
      <tr><th>Topics created:</th><td><?php echo $topics; ?></td></tr>
   </table>
</div>

and your controller could declare the variables and then include this view in the output.

Upvotes: 0

user743234
user743234

Reputation:

Basic idea to initialize your understanding :) if you want the view class to be more powerful, you need to develop it further.

view.php

<?php
class View {    
    function __construct($tpl) {
        include $tpl;
    }
}
?>

handler.php

<?php
class Handler {
    function __construct() {}
    function process($post) {
        echo $post;
    }
}
?>

bootstrap.php

<?php
require('view.php');
require('handle.php');

$view = new View('form.html');
$handler = new Handler();

if (isset($_POST['login'])) {
    $handler->process($_POST['username']);
}
?>

Upvotes: 3

Rizwan Yahya
Rizwan Yahya

Reputation: 350

i think first understand the MVC in detail, then play with some existing frameworks, may be start with codeigniter, its simple to understand one get the details then create your own(if required!)

Upvotes: 0

bigblind
bigblind

Reputation: 12867

Views should not be classes, views should be pieces of PHP and HTML or any other format that you'd like to output, which can than be invoked by a controller.

Upvotes: 0

Related Questions