Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Rest API Nested Resources

edited December 2013 in Framework

As far as I can tell, there is no support for nested resources in the provided rest api classes... @jbroadway, do you have something in mind for implementing this?

//Example
articles/api/article/123/comment/1

Comments

  • edited December 2013

    While I am at it... how do I pull the current Users id/name from a post_apicall() function.

    //Example
    public function post_article(){
      User::val('id'); // This is always equal to null, even though I have a logged in user
    }
    
  • Since the routing will get matched to your get_article() method, for now you'll have to check for the additional parameters in that method and pass them along:

    public function get_article ($id, $comment = null, $comment_id = null) {
        if ($comment == 'comment' && $comment_id) {
            return $this->article_comments ($id, $comment_id);
        }
        // party on, wayne
    }
    

    As for User::val(), those values won't be set until the session is initialized by a call to User::is_valid() or User::require_login().

  • Glad to know that I solved it the correct way, I was just hoping for something a bit cleaner.

    Thanks!

  • Huh. The user stuff still isn't working. I am probably missing something easy.

    https://gist.github.com/shortjared/7881913

  • Hmm, that's strange. Here's a simplified version that seems to work for me:

    <?php
    
    namespace test;
    
    use User, Restful;
    
    class API extends Restful {
        public function get_loginrequired () {
            if (! User::is_valid ()) {
                return $this->error ('Must be logged in.');
            }
    
            return User::val ('name');
        }
    }
    
    ?>
    

    I threw that into apps/test/lib/API.php and called it in apps/test/handlers/api.php like this:

    <?php $this->restful (new test\API); ?>
    

    Heading to /test/api/loginrequired gave me the error, then my name when I was logged in afterward. Let me know if that works for you, or helps sort out what's not working.

  • Thinking about more complex routing for things like nested resources, what if we added something like this:

    <?php
    
    namespace myapp;
    
    class API extends \Restful {
        public $custom_routes = array (
            'GET article/%d/comment/%d' => 'article_comment',
            'POST article/%d/comment' => 'post_article_comment',
            'ALL article/%d/author' => 'article_author'
        );
    
        public function article_comment ($article_id, $comment_id) {}   
        public function post_article_comment ($article_id) {}
        public function article_author ($article_id) {}
    }
    
    ?>
    

    Which should be easy enough to implement, and if $custom_routes isn't defined it would just use the default behaviour. The %d markers could be converted to their regex equivalents, and some regex support could be had in the routing as well.

    What do you think?

  • edited December 2013

    I didn't declare the namespaces... That is probably where I messed up. I'll give that a shot tomorrow.

    Custom routing would definitely be nice, while I think you want to keep elefant clean, it's the feature I miss the most that other frameworks have solid support for.

  • Just committed a basic implementation of the custom routes:

    https://github.com/jbroadway/elefant/commit/d42a862e2cbb8fe21b99785d82e5c107ef2ea2a2

    Let me know what you think! :)

  • Code looks like what you were talking about, I will give it a try later today.

  • I just realized the HTTPShell App doesn't seem to work with the authentication. Thus why my GET requests were working from my browser, however POST was not through the HTTPShell App.

  • Does it work if you add the username and password parameters to the POST request? It'll re-authenticate on each request, but should get past the User::is_valid()'s.

    Would be handy if the shell could remember the cookie though, so I added an issue on github for that:

    https://github.com/jbroadway/httpshell/issues/1

Sign In or Register to comment.