Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post request mapping #10

Closed
eduh opened this issue Oct 10, 2011 · 8 comments
Closed

Post request mapping #10

eduh opened this issue Oct 10, 2011 · 8 comments

Comments

@eduh
Copy link

@eduh eduh commented Oct 10, 2011

I have a testclass as below and I'm wondering why I can't use the function anotherpost() with a post request
So get+anotherget are returning as expected when using a GET request.
When I do a POST request with the url ....../test/anotherpost it returns 404 ...or am I misunderstanding something in the concept of mapping the methods ?

<?php
class Test
{

    function post() 
    {
        return "Hello Post";
    }
    function anotherpost() 
    {
        return "Hello Another Post";
    }
    function anotherget() 
    {
        return "Hello Another Get";
    }
    function get() 
    {
        return "Hello Get";
    }   
}
?>
@ghost ghost assigned Luracast Oct 11, 2011
@Luracast
Copy link
Collaborator

@Luracast Luracast commented Oct 11, 2011

I apologize for the lack of clarity in my example, I will update it to make it very clear

here is how the automatic routing is done

We use prefix get, put, post, and delete to map functions to respective HTTP methods

GET is the default HTTP method so all public functions with out one of these prefix will be mapped to GET request

<?php
class Test
{

    function post() 
    {
        #mapped to POST test
        return "Hello Post";
    }
    function postAnother() 
    {
        #mapped to POST test/another
        return "Hello Another Post";
    }
    function getAnother() #we can also write it as another()
    {
        #mapped to GET test/another
        return "Hello Another Get";
    }
    function get() #we can also write it as index() or default()
    {
        #mapped to GET test
        return "Hello Get";
    }   
}
?>

Hope this makes it clear

@eduh
Copy link
Author

@eduh eduh commented Oct 11, 2011

Thank you. This did the trick. btw: the comment tag '#' is deprecated and will generate parse errors in php6

@electrical
Copy link

@electrical electrical commented Oct 17, 2011

Just something to keep in mind.

If you for example have 2 API functions like

/test/$id
test/another/$id

make sure that the second function comes first in your class.
I had the headache that it didn't work the other way around.

I got this explanation from the creator:

When we have a ambiguous situation, the order in which the functions are written becomes important. Less ambiguous ones should be placed on top.

@eduh
Copy link
Author

@eduh eduh commented Oct 17, 2011

Yep, I ran into that problem as well. I'm curious what happens if we have

function test($param1,$param2='something')
and
function another($param1)

If you want to call method one (test) you could do something like /test/another/hello
...and what happens then. Going to test this but it is a tricky thing

@Luracast
Copy link
Collaborator

@Luracast Luracast commented Oct 17, 2011

What you are describing wont have any problem at all as /another/... and /test/another/... are different URLs

but if we change test as index or get then it will be mapped to the root and create ambiguity

@eduh
Copy link
Author

@eduh eduh commented Oct 23, 2011

Yes...my example should have been as below
And the url is then for both get+post the same /test/another/param1
This causes no conflict when posting or getting.

<?php
class Test
{
    function getAnother($param1,$param2='something')
    {
       return "Hello Test data=$param1 and $param2";
    }

    function postAnother($id) 
    {
        return "Hello Another Post data=$id";
    }
}

@kzap
Copy link

@kzap kzap commented Jan 16, 2012

is there an easy way to make the same function work for both POST and GET or do we have to create another function like

function get() { 
    return 'succces'; 
} 
function post() { 
    return $this->get(); 
}

?

@Luracast
Copy link
Collaborator

@Luracast Luracast commented Jan 16, 2012

All the above are by convention. Another way is to configure it the way we want

<?php
class Something {
    /*
    * @url GET /
    * @url POST /
    * @url GET /custom/route
   */
    function get() { 
        return 'succces'; 
    } 
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants