By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,304 Members | 3,132 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,304 IT Pros & Developers. It's quick & easy.

MVC: some questions on views

P: n/a
Hello,

I 'm writting an application using an MVC approach of mine (not with a
framework). Notice that I 'm not writting a framework but an application
with such an approach.

I 'm having trouble understanding how views work. I have understood
(from web reading) that MVC is a flexible approach and not something
strict so I want some advice.

I have index.php as a front controller. Depending on the module being
asked (index.php?module=professors for example) I load an appropriate
class controller.

Then in index.php I have a $controller->Process() function and a
$controller->view->Display() where view is a view defined in the controller.

But how do you define a view? Lets assume I am a teacher with a login in
the system and visit the site. A login in form should be presented upon
first visit to login. Is this a view by itself? Then if you login you
are shown a menu and a default page. This page holds a list of notes you
have uploaded, and you can upload/delete/edit the data. Is this a page a
single view? And what about the menu of the user. Is it a separated view
included from the main view?

I find it a little bit complicated to undestand. And to give you an
example this is my view now:

class gView {
var $header;
public $title;
public $jsscripts;
public $metakeywords;
public $metadescription;
public $todisplay;

function header(){
$title=$this->title;
include('staticpages/top.php');
}

function footer() {
include('staticpages/footer.php');
}

function Display() {
$this->header();
print $this->todisplay;
$this->footer();

}
}

The $controller->Process() function loads the appropriate function and
sets the $view->todisplay string. Just to clarify that each controller
holds also the buisness model is the class. I don't use a separate model
class for each controller.

Any help/hint would be valuable.

Thanks a lot
Harris
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Harris Kosmidhs wrote:
Hello,

I 'm writting an application using an MVC approach of mine (not with a
framework). Notice that I 'm not writting a framework but an application
with such an approach.

I 'm having trouble understanding how views work. I have understood
(from web reading) that MVC is a flexible approach and not something
strict so I want some advice.

I have index.php as a front controller. Depending on the module being
asked (index.php?module=professors for example) I load an appropriate
class controller.

Then in index.php I have a $controller->Process() function and a
$controller->view->Display() where view is a view defined in the
controller.

But how do you define a view? Lets assume I am a teacher with a login in
the system and visit the site. A login in form should be presented upon
first visit to login. Is this a view by itself? Then if you login you
are shown a menu and a default page. This page holds a list of notes you
have uploaded, and you can upload/delete/edit the data. Is this a page a
single view? And what about the menu of the user. Is it a separated view
included from the main view?
The idea behind MVC is to separate data from it's presentation.
Controller acts as a director, listens to user and decides what to do.

View should be only responsible for presentation data it was given.
As so, you should not decide here, what is the state of navigation, or
if user needs to login first. Those are both controller responsibilities.

Your controller should process all data in context of your application
state, and pass only the result to view, for sole purpose of
presentation this data (rendering).

It's not said, that you must render single view per request. Your output
might be composed of many views. You can render your menu, other bocks
and main content, compose those (within controller) and then send it to
user.

In some of the frameworks, you also has a Layout module, this is a kind
of view decorator/composer. It takes separate views, and combines them
into complete web page. You can think of it as a html template with
options to set blocks. Ie.:
$layout->setMenu($someMenuViewNameOrRenderedString);

As you can see, controller has a lot of work with handling request, this
is why there is a separate 'MODEL' component, that should hold data
handling logic. Just to keep things simple and readable. You are free to
do what is best for you, but this pattern has been proven right many times.

On the side note, it's good to use a framework. Lets leverage the fact
that there are so many good programmers that are willing to do the job
for us. Hell, we might even learn something from them.

Hope this helps you a bit

best regards
Piotr N
Jun 27 '08 #2

P: n/a
The $controller->Process() function loads the appropriate function and
sets the $view->todisplay string. Just to clarify that each controller
holds also the buisness model is the class. I don't use a separate model
class for each controller.
Why would you do that ? The beauty of a MVC structure is that you
seperate the model and view

This is how I would do it

class ProfessorModel extends Model{
// etc
}

class CourseModel extends Models{
//etc
}

class ProfessorController extends Controller{

public function index($id){

// get Professor Info
$oProfessor = new ProfessorModel($id);

// get all Courses given by this professor
$oCourses = new CoursModel($Professor->getId());

// etc
}
Then pass the data ($oProfessor and $oCourses) to a View and process

Jun 27 '08 #3

P: n/a
floortje wrote:
>
>The $controller->Process() function loads the appropriate function and
sets the $view->todisplay string. Just to clarify that each controller
holds also the buisness model is the class. I don't use a separate
model class for each controller.

Why would you do that ? The beauty of a MVC structure is that you
seperate the model and view

This is how I would do it

class ProfessorModel extends Model{
// etc
}

class CourseModel extends Models{
//etc
}
yes I understand this. But what does Model consists of? I mean what are
the basic functionalities that can be extended?
Unless by ProfessorModel you mean a class that consists the name, id,
and general fields of professor master data...
Jun 27 '08 #4

P: n/a
Harris Kosmidhs wrote:
floortje wrote:
>>
>>The $controller->Process() function loads the appropriate function
and sets the $view->todisplay string. Just to clarify that each
controller holds also the buisness model is the class. I don't use a
separate model class for each controller.

Why would you do that ? The beauty of a MVC structure is that you
seperate the model and view

This is how I would do it

class ProfessorModel extends Model{
// etc
}

class CourseModel extends Models{
//etc
}

yes I understand this. But what does Model consists of? I mean what are
the basic functionalities that can be extended?
Unless by ProfessorModel you mean a class that consists the name, id,
and general fields of professor master data...
Most people extend a database abstraction layer as their model base.
So they can make save, update an so on on the data.

You can also add logic that is common across your models

So you can think of it like this

class Model {

//so your model can load data he needs
protected function getById();
public function getByName();

//so model user can change the data
public function save();
public function delete();

}

class PersonModel extends Model {
public function getName()
public function getLastName()
public function getDayPlan()
}

class ClassModel() {
public function addStudent(StudentModel $student);
public function removeStudent(StudentModel $student);
public function getProfessor();
public function setProfessor(ProfessorModel $processor);
}

class ProfessorModel extends PersonModel {
public function getClasses()
public function addClass(ClassModel $class,$timeStarts,$duration);
public function removeClass(ClassModel $class);
}

class StudentModel extends PersonModel {
public function addNote($note, ClassModel $class);
public function kickOut();
public function graduate();

}

ect ect :)

If your models are well designed, and have good, consistent api, you
will have no trouble dealing with them inside controllers. And your
controllers will be easy to read and maintain

best regards
Piotr N
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.