SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 30 of 30
  1. #26
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Gambler Z View Post
    Why? What advantages does it have?
    Good question. Hard to answer in brief.

    HTTP is a very common protocol. By using the protocol as close to its letter, you gain the benefit of interoperability with all the pieces of soft- and hardware in existence that speaks HTTP. It's a central idea in HTTP that URL's should be thought of as resources, and these resources then allow certain operations to be performed on them. Since these operations are well known, intermediaries can make assumptions. For example, a proxy might know that a GET request is safe to repeat, if an error occurred, but a POST request is not. If the actions are instead encoded into the URL, the meaning of the request becomes opaque to anyone but the endpoints. That's basically the difference between using a well described standard or using a proprietary protocol. The latter has overhead.

    These ideas was inherent in the HTTP protocol, but has since been formalised better in the principles of REST.

    Quote Originally Posted by Gambler Z View Post
    Also, how would you handle things like logging in or digging an article?
    The "language" of resources + actions is actually surprisingly articulate. There are corner cases, but for - say - 90% of all operations, you can easily fit it in to this dogma. For the remainder, you'll have to be inventive, but you can often redefine your model to make it map better to the REST principles. For example, instead of "digging an article", you could think of the action as "creating a new dig for an article"; So the action might be mapped as a POST request to /articles/$article_id/diggings, which would likely respond with a redirect to the created resource, namely /articles/$article_id/diggings/$dig_id.

  2. #27
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The topic (and my question) is about URL style, though. GET, POST, etc can be used without structuring application's URLs to look like static files.

  3. #28
    SitePoint Evangelist
    Join Date
    Aug 2005
    Location
    Winnipeg
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's good practise to let URLs identify resources, not actions. The following violates this:
    RESTful URI's have always confused me, but that sentance really cleared things up for me.

    One question though, how then does/would a router determine which action to dispatch too, I assume by checking isMethod() of a request object?

    As my framework stands right now, each URI maps to a action (standard practice I think) but now I need that additional layer in my router or somewhere to determine whether an action maps to a GET, POST, PUT, DELETE, etc???

    How do you deal with this in Konstrukt? Would you mind offering a simplified example?

    Cheers,
    Alex
    The only constant in software is change itself

  4. #29
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PCSpectra View Post
    One question though, how then does/would a router determine which action to dispatch too, I assume by checking isMethod() of a request object?
    That depends entirely on the framework/router. In Rails for example, you would specify the method as part of the route. Eg.:
    Code:
    map.connect 'contact/:id', :controller => 'contacts', :action => 'show', :conditions => { :method => :get }
      map.connect 'contact/:id', :controller => 'contacts', :action => 'create_comment', :conditions => { :method => :post }
    The above maps GET /post/42 to the method ContactsController#show while POST /post/42 goes to ContactsController#create_comment. Basically, the router doesn't just look at the URI, but also at the method.


    Quote Originally Posted by PCSpectra View Post
    How do you deal with this in Konstrukt? Would you mind offering a simplified example?
    Konstrukt is basically a router on steroids. You can dispatch on anything, including http method. A simple example could be:

    PHP Code:
    class components_Contact {
      function 
    renderHtml() {
        return 
    "<p>foo</p>";
      }
      function 
    postForm() {
        
    // Do stuff with $this->body() here
        
    $this->posts->save($this->body());
        
    // redirect back to self (redirect-after-post)
        
    return new k_SeeOther($this->url());
      }

    You can also handle it on a lower level:

    PHP Code:
    class components_Contact {
      function 
    GET() {
        return 
    "<p>handle get</p>";
      }
      function 
    POST() {
        return 
    "<p>handle post</p>";
      }

    The first example is preferred, since it leaves Konstrukt to perform content-type negotiation transparently behind the scenes.

    There's a more complete example at http://www.konstrukt.dk/getting-started-part2.html

  5. #30
    SitePoint Evangelist
    Join Date
    Aug 2005
    Location
    Winnipeg
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That depends entirely on the framework/router. In Rails for example, you would specify the method as part of the route. Eg.:
    So obvious and yet I totally didn't see it, thanks.
    The only constant in software is change itself


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
  •