Re: [SMARTY] A question about Smarty and MVC...
| From: | Jason Sweat | Date: | Fri, 14 Mar 2003 04:19:08 +0000 |
| Subject: | Re: [SMARTY] A question about Smarty and MVC... | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-8639@lists.php.net to get a copy of this message | ||
John,
I use separate classes for each element of the MVC, that way there is a very
clear separation.
My controller object looks at $_GET['view'] and decides what view is
appropriate, including a default view if nothing is specified. The
controller also has mapping between the requested views and the php file
containing the class definition that implements the view. The controller
creates a Smarty object, requires the view class, and creates a View object,
and then calls
$view->Render($smarty);
Inside of the view, I allow the view class to retrieve $_GET[] values
specific to that view. The view render method would then create an instance
of the model it needed to get the id from, i.e.
class ThisView()
{
function Render(&$smarty)
{
$model = new Model;
$id = $model->GetIdByName($_GET['name']);
$smarty->assign('id',$id);
$smarty->Display('thisviewtemplate.tpl');
}
}
This structure lets me do some global assignments to the $smarty object in
the controller itself, and then lets each view work with the appropriate
models to get the data required to display.
If you do not go to the effort of structuring everything in classes, it will
be only a logical distinction in your mind as to which role the particular
line of code you are on is performing.
HTH. Did that get close to the answer you are looking for?
Jason
----- Original Message -----
From: "John Wells" <jb@sourceillustrated.com>
To: <sweatjejm@mchsi.com>
Cc: <smarty-general@lists.php.net>
Sent: Thursday, March 13, 2003 9:48 PM
Subject: Re: [SMARTY] A question about Smarty and MVC...
> Guys, sorry about the incomplete message earlier...I hit the wrong key
;-).
>
> Jason,
>
> Thanks for the links. I've read the articles. Good, but I don't think
> they address my initial question.
>
> Let me restate it a bit:
> If I were to design an MVC application with Smarty/PHP, could I consider
> the script that accepts user input to be the controller, Smarty to be the
> view, and any classes the controller calls based on user input (including
> the business logic and database classes) to be the view?
>
> So, if I have the following code:
>
> <?php
>
> // get name from request
> $name = $_REQUEST["name"];
>
> // get id from database
> $id = myDatabaseObject.someDatabaseQuery($name);
>
> $smarty = new Smarty;
>
> $smarty->assign('id', $id);
>
> $smarty->display('viewId.tpl');
> ?>
>
> Then, if I understand correctly, the above script would be viewed as the
> part of the Controller, because it accepts user input and forwards it to
> the appropriate places. The myDatabaseObject would be considered part of
> the model, and the Smarty class and tpl would be considered the view.
>
> Is this totally off base? If so, how?
>
> Thanks!
>
> John