last day (15 days later) » 

4:18 PM
32
A: ACL implementation

tereškoFirst part/answer (ACL implementation) In my humble opinion, the best way to approach this would be to use decorator pattern, Basically, this means that you take your object, and place it inside another object, which will act like a protective shell. This would NOT require you to extend the orig...

 
I just learned more in 5 minutes rereading this, than I have in months. Would you agree with: thin controllers dispatch to services which collect view data? Also, if you ever accept questions directly, please send me a message.
 
I partially agree. The collection of data from view happens outside MVC triad, when you initialize Request instance (or some analog of it). The controller only extract data from Request instance and passes most of it to proper services (some of it goes to view too). Services perform operations that you commanded them to do. Then, when view is generating the response, it requests data from services, and based on that information, generates the response. Said response can be either HTML made from multiple templates or just a HTTP location header. Depends on the state set by controller.
To use a simplified explanation: controller "writes" to model and view, view "reads" from model. Model layer is the passive structure in all of Web related patterns that have been inspired by MVC.
@Stephane , as for asking question directly, you can always message me in twitter. Or were you question kinda "long-form", that cannot be crammed in 140 chars ?
 
Reads from the model: does that mean some active role for the model? I've never heard that before. I can always send you a link via twitter if that is your preference. As you can see, these responses turn into conversations quickly and I was trying to be respectful of this site and your twitter followers.
 
No , it means that view has an active role. Views are the part of application that's responsible for presentation logic. They request data (which is why "read" was in quotes) from model layer through services, and generate response. When dealing with web, you sually will ed up with 1:1 relation between controllers and views.
@Stephane , btw , you might also find this post relevant to your research.
 
Thank you that was great. I'm confused about 1:1 controller:view. Are you talking about a separate class or just a separate method? Also, I always thought controllers called the view. Controller->Model Layer->Controller->View. It seems like it's more like: Controller->Model Layer->View. I always wondered why people talk about thin controllers. Maybe this is why that confused me.
 
4:18 PM
Views are classes, that are responsible for presentation logic, and each view juggles several templates. Controller does not call the current view. It only changes view's state.
 
Hmm.. In don't understand how a controller can change a view's state, but doesn't call it. Isn't the controller lower in the stack?
 
4:52 PM
namespace Controller;
class FooBar
{
    protected $view;
    protected $serviceFactory;

    public function __construct( $view, $serviceFactory )
    {
        $this->view = $view;
        $this->view = $serviceFactory;
    }

    public function getDocuments( Request $request )
    {
        $this->view->setLayout( $request->getQuery('form') );
        // documents can be presented either as a list or grid

        $this->view->setOffset( $request->getQuery('offset') );
        // for pagination
@Stephane , something like this
controller changes the state of view, but it should not be responsible for rendering the response
 
That makes sense. With this design, where would the model layer get called? It looks like the serviceFactory would do that? Or would the view get called further down in getDocuments?
Thank you by the way.
 
5:13 PM
for simple GET requests there is no real point in changing the state of model layer
you would be just retrieving data from model and that is done in the views
and yes , the service factory would be used , when you need to mess with the model layer , but that usually would happen when user sends a POST, PUT or DELETE request
 
So the view gets the request? Or the controller calls something like setLayout but for the rest of the elements?
 
no
there are usually only few parameters that you send in a HTTP request
and most of the $_GET fields would be passed to the view
but that's more like a "rule of thumb" then as strict law
take for example StackOverflow page
when you view the main page , there are no additional GET or POST parameters sent
when you view your profile in SO , then only parameter sent is the UserID
the point here is that , usually there won't be such thing as "rest of the elements"
 
So each "page" would have a function like getDocuments in a controller and it would also call view->getDocuments? Meaning, no data gets returned to the controller and passed on to the view. That's what I used to think and I get why that's not optimal.
 
5:35 PM
no , view does not have such method
the getDocuments() would be a controller's method
it would just set up all the needed signals so that view can retrieve list of documents from model layer
.. i kinda suspect that it was a bad name to choose
 
That actually makes sense to me. I'm starting to see. No, it's good.
 
$view = new DocumentsView( $serviceFactory );
$controller = new DocumentsController( $view, $serviceFactory );

$controller->getList( $request );
 
Ok, so how does the view get invoked? Where is the call placed? If I'm trying to load user documents 1234, where does the view get called?
ahhh
wow
 
that would be what you call from http://foo.bar/documents/list
 
any why does the controller configure the view if the view is going to be getting the data? How is that a separate concern?
 
5:39 PM
when you submit form to get Documents controller , you actually execute $controller->postItem()
 
which would send the post not to the view, but to the service layer?
which links to the view?
 
$view = new DocumentsView( $serviceFactory );
$controller = new DocumentsController( $view, $serviceFactory );

$controller->postItem( $request );

$view->prepare();
$view->sendResponse();
 
this is completely the opposite of how I was seeing it
my brain is catching up still
 
did you look at there code example here : stackoverflow.com/a/5864000/727208
 
I see. And in the controller for postItem, how would that link to the model or the DAO or whatever?
 
5:43 PM
lets see
 
I did look at that. LIke I said, a lot of this is a flip for how I was doing it, so it doesn't immediately register. And without a full path from request to page, I can't always see the details.
 
... if you are posting a new document to the Documents controller
you would use some "Library" service, and send data to it
 
My projects are LARGE with hundreds of tables in procedural code and to go from that to this it's a little confusing.
 
and then tell view that there was a POST request received
 
Why would you want to tell the view? So that it can display a "Document posted" message for example?
Seems like a job for an observer
but maybe that just complicates it.
 
5:46 PM
public function postItem( $request )
{
    $list = $this->serviceFactory->build( 'Library' );
    $list->addDocument( $request->getPost( 'title' ),
                        $request->getPost( 'content' ) );

    $view->responseType( $request->getMethod()  );
}
the service would attempt to save/create new document and view would receive indication that there was a POST request , and it need to send HTTP Location header , to trigger a redirect
 
redirects happen in the service layer, not the controller? Interesting.
 
if the attempt of creatign the document failed at some point, model layer would acquire an error state
@Stephane NO
redirect happens in the view
jeez
the header is just a type of response that view generates
 
I see. Even if the response is just a redirect. Ok, that makes sense.
Sorry if this is frustrating. I really appreciate the help.
 
the main thing you have to understand about MVC and MVC-inspired patterns is that it actually consists of two layers
model layer and presentation layer
model layer deals with all the business logic , it contains domain object, data mappers, repositories , dao's etc.
the presentation layer consists mostly of views, controllers and templates (or layouts, in desktop apps)
presentation layer creates the user interface
controller deals with user requests
view makes the responses
 
I think what I'll do is type up some code and put up a link. I think that will clear up where I'm missing things. I understand things in theory (maybe), but the application for me is harder because of my previous experience and previous information about MVC (that I may have learned incorrectly)
 
5:55 PM
what you have learned was the Rails adaptation of MVC pattern
when Rails was originally created , it was supposed to be a prototyping framework , and it was really good for that
but since it was never mean for long-term development , there were a lot of compromises
 
I want to learn this correctly so it applies outside of http.
 
when you are dealing with MVC for desktop application , there is one additional aspect : the view observes changes in model
which is impossible extremely hard and impractical on web
and , i need to run now
shops close in an hour and i want to buy a beer
 
ha enjoy your weekend
thanks again
 

last day (15 days later) »