AltoRouter icon indicating copy to clipboard operation
AltoRouter copied to clipboard

Added alias mapping methods

Open dvdmarchetti opened this issue 10 years ago • 6 comments

Added get, post, patch, put and delete methods as a shorthand to map route to a target with a single verb.

dvdmarchetti avatar Sep 11 '15 20:09 dvdmarchetti

Every function call is expensive. What is the benefit?

tekkla avatar Sep 13 '15 15:09 tekkla

It's just shorthand methods. The benefit is less to write, and more intuitive mapping. It's just an advice, if you don't think these are useful you can also close and ignore this pull request. Anyway, keep up this good work.

dvdmarchetti avatar Sep 22 '15 21:09 dvdmarchetti

I have these methods in my own implementation, which extends of AltoRouter, but I noticed I rarely use them.

But I'm not opposed to this addition. Only one thought, is it necessary to document all the @param on every method, or is their a way to refer to the @param documentation of the map method?

koenpunt avatar Sep 23 '15 15:09 koenpunt

For those interested:

I've made this request in what I suspect is a simpler method. Instead of created 5 new methods within the class you can just use the __call magic method.

In my version of altorouter I've added:

/**
 * Magic method to route get, put, post, patch, and delete
 * to the map method. Example usage:
 * $router->get($route, $target, $name);
 * $router->put($route, $target, $name);
 * $router->post($route, $target, $name);
 * $router->patch($route, $target, $name);
 * $router->delete($route, $target, $name);
 *
 * @param string $name      What are we calling.
 * @param string $arguments The arguments less the method.
 *
 * @return void
 */
public function __call(
    $name,
    $arguments
) {
    $name = strtolower($name);
    $validTypes = array(
        'get' => 'GET',
        'put' => 'PUT',
        'post' => 'POST',
        'patch' => 'PATCH',
        'delete' => 'DELETE'
    );
    // If method type is invalid don't do anything.
    if (!isset($validTypes[$name])) {
        return;
    }
    // Prepend the type to our arguments to pass to the map method.
    array_unshift(
        $arguments,
        $validTypes[$name]
    );
    // Pass to the map method.
    call_user_func_array(
        array($this, 'map'),
        $arguments
    );
}

Hopefully this helps, and it lessens the "repeat" code/comments.

mastacontrola avatar Apr 02 '17 15:04 mastacontrola

call_user_func_array will have more overhead than the solution proposed in this PR

koenpunt avatar Apr 02 '17 16:04 koenpunt

While it may have some overhead, it would not be much greater than just calling a mapped alias. In our case, you can only call a single method at a time, so processing at an individual level would be very minimal.

As the mapping only occurs if the function does not exist, and we are only allowing our 5 base routes, I still think this is more suitable than 5 separate methods. Having the extra methods means memory must be allocated at call time. (Not much of course, but still). Using the magic method, only the call function has to reserve some memory, and our mappings are automatically appropriate.

It minimizes the overall code repeats as well and lessens the potential of mistakes.

mastacontrola avatar Apr 02 '17 17:04 mastacontrola