SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 27
  1. #1
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Views and helpers

    Hi,

    I'm trying to find the easiest and simplest way to get helpers into my views without having to:

    add my controller to the view (maybe the best way?)
    put in a bunch of extra methods
    use global functions

    I basically want to just build urls, and the controller knows how to do that. So, I could just construct a new url and pass it to the view everytime, but there has to be a better way? Is it considered bad practice to pass the controller to the view to get a little request help? If I did that, I could even use the view as an adapter/wrapper and not let the templates do $this->controller->url() but $this->url()

    - matt

  2. #2
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    I think codeigniter, does the controller method. I don't really like it though.

    I guess the question is: How do you get plain variables to your view? Which ever way you do that, I would use that method to either pass the entire object, or the return of a particular helper method.
    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nothing wrong with referencing the controller for URL building IMO, but that's about the only aspect I can think of for which you'd need controller access. Your only concern could be the dependencies you introduce in template code. This shouldn't be an issue if it's acceptable that your templates can only be interpreted by a specific view.

  4. #4
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mwmitchell
    I basically want to just build urls, and the controller knows how to do that.
    I have a similar issue, which I've resolved by passing the controller to the view, but wrapping the url calling function, as you've mentioned. But I think a better solution is to put the url building functionality into a seperate object, and just pass that, which is what I'll be doing soon.

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was thinking that the functionality required for building a URL could (would?) be Route specific but then you may need several Routes for a given View

    I think that from a design point of view, it'd be a bad choice to pass in the Controller to the View, but I don't have a read made solution either, sorry.

  6. #6
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Should an MVC controller know how to create urls?

  7. #7
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about injecting a registry into both the controller and the view, which would hold your url builder?
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  8. #8
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by McGruff
    Should an MVC controller know how to create urls?
    I guess it depends on the context. If it is a controller that is selected to handle a specific page, then I don't think it's a problem if it knows how to handle *relative* urls. Am I way off base here?

    - m

  9. #9
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by McGruff
    Should an MVC controller know how to create urls?
    I don't think so. The only time I could invision it needing to would be for a header redirect, which should be the responsibility of a Response object (a separate object that the controller can use).

    I can't think of a single example where the controller NEEDS such a responsibility... anyone?

  10. #10
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    > What about injecting a registry into both the controller and the view, which would hold your url
    > builder?

    Why would you want to do that; You are just adding more dependencies I imagine? Another question that begs my believe, is that of why does the Controller know about URL generation?

    Sure, the Controller knows about the request, and how to manipulate it but to my thinking, not to the extent of generating a URL, when I've had more time to think it over

    Maybe the URL generation has more to do with the Request (object) it's self if anything...

  11. #11
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by McGruff
    Should an MVC controller know how to create urls?
    That's a good point. In my case the answer is that it only has a bit of knowledge of URL creation; The majority of the work is taken is done by the URL Routing system, and the controller simply has a method that wraps the call to the route system, with a bit of additional logic. The main reason for this is that the routes system is currently a static registry, and since I didn't want to couple it to all the parts of the application that needed to generate URLS, I passed the controller instead. It's a flawed design, and I know how to fix it, I just haven't gotten around to it.

  12. #12
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    Why would you want to do that; You are just adding more dependencies I imagine?
    I was actually following this train of thought.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  13. #13
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    > and the controller simply has a method that wraps the call to the route system, with a bit of additional
    > logic.

    Care to share what you mean by this additional logic? What does it do in relation to your routes logic

  14. #14
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    > I was actually following this train of thought.

    The penny drops... But that did cross my mind, as the term injector was mentioned, but then you went on to suggest a registry; A Registry is different (in a number of ways) from a DI container

    But yes, your thought was on track in that case.

  15. #15
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    haha, ya, sorry. didn't mean to confuse things w/the vernacular there
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  16. #16
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by mwmitchell
    I guess it depends on the context. If it is a controller that is selected to handle a specific page, then I don't think it's a problem if it knows how to handle *relative* urls. Am I way off base here?

    - m
    I think routes should handle direction and redirection only. Not rendering. On event, you redirect. When you render there is no event, the event would be clicking the link, which then gets iterpreted by routing and you are directed accordingly.

    I think at most the url helper could ask routes where it is currently routed to. But even then, I think it breaks the domain of the routes class.

    This is how mine would look like.

    Normal:
    url parser -> routes ->
    Render Url:
    url parser -> url handler -> renderUrl ()
    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users

  17. #17
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    > and the controller simply has a method that wraps the call to the route system, with a bit of additional
    > logic.

    Care to share what you mean by this additional logic? What does it do in relation to your routes logic
    The controller has a url_for method that you call to generate a url, with two arguments. If the first argument is an array, it calls the routes system, using the array as parameters for the desired url (e.g. array('controller'=>'news', 'action'=>'index')). If you pass a string instead, the method will use it as a relative link, with the second parameter being querystring options.

    Clearly, this could easily be refactored (put all the logic in the routes), I just haven't gotten around to it...

  18. #18
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Quote Originally Posted by Dr Livingston
    it's self
    Itself, for crying out loud. Hardly a reply goes by without you using the word - I think you oughta learn how to spell it, too.

  19. #19
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    it's self

  20. #20
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Quote Originally Posted by Ezku
    Off Topic:

    Itself, for crying out loud. Hardly a reply goes by without you using the word - I think you oughta learn how to spell it, too.
    That is not the one I choke on each time I see it.

    proberly ??

    No offence intended Dr L, I have kept it to myself for years

    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  21. #21
    Non-Member melancholic's Avatar
    Join Date
    Nov 2004
    Location
    Australia
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:


    Quote Originally Posted by sweatje
    Off Topic:



    That is not the one I choke on each time I see it.

    proberly ??

    No offence intended Dr L, I have kept it to myself for years

    LoL I second that


  22. #22
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Jason, no problem, I am set in my ways I suppose (here we go again) so by all means, it's not a problem for me. Proberly

  23. #23
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by McGruff
    Should an MVC controller know how to create urls?
    To separate concerns it's probably wise to use an object that knows how to build URLs, i.e. a generic URL builder or a request router/mapper. If you want to use request parameters to 'fill in the blanks' when generating URLs, it can be convenient to use a controller method to pull things together IMO. If you want to access this in a view you'll need a controller reference.

  24. #24
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, I'm confused now!

    I think that I'll take the most resonablly clean way and just add a method to the view class called url(). The controller is inherently tied to the urls (it's the way my system works) so I'll also pass a reference of the controller into the view. But the templates will not use the controller reference, it'll be private.

    Another example is how I'm handling the Rails like flash() idea. I have setFlash() and getFlash() methods. The getFlash() mehod would be used in the view obviously. I *could* use getFlash() in the controller and pass in a new variable called "flash_msg" or something. But that's more work! It seems much more clear to just call $this->getFlash() in the templates. Which means I'd add another method called getFlash() which just decorates the controller getFlash() method.

    These are both the same kind of problem. Having parallel methods in the controller and view classes seems clean to me.

    Any more opinions? Thanks for the help people.

  25. #25
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, scrap that. I think passing variables into the view is much better at this point. If something changes in this system, then I'd have to go thru all of the templates and remove $this->url() blah blah. I'm sticking with passing values into the view and letting the templates do $this->shop_url for now. This stuff is always so back and forth for me. Ugh!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •