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

Dynamic resource response in ResourceController #7

Closed
thomasowow opened this issue Feb 22, 2019 · 1 comment
Closed

Dynamic resource response in ResourceController #7

thomasowow opened this issue Feb 22, 2019 · 1 comment
Labels
enhancement New feature or request

Comments

@thomasowow
Copy link
Member

thomasowow commented Feb 22, 2019

Currently when a custom manager method needs to be called to only retrieve and display its contents, a custom controller needs to be created. 90% of the cases the only difference between the ResourceController and this custom controller is the method that is being called to retrieve these results.

Eg:

class PostController extends ResourceController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     *
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function index()
    {
        $this->authorize('view', $this->resourceModelClass);

        $resourcesPaginated = $this->resourceManager->paginateForUser(currentUser()); // <- the only difference

        $resources = resource($resourcesPaginated, true);

        $resourcesPaginated->setCollection($resources->collection);

        return ok($resourcesPaginated);
    }
}

A suggestion would be to implement the __call method to allow a non Response type to be returned by all resource methods. The following pseudo example could make things a little more clearer:

class PostController extends ResourceController
{
    public function index()
    {
        // Instead of implementing all duplicate code we will just return a
        // LengthAwarePaginator type.
        return $this->resourceManager->paginateForUser(currentUser());
    }
}

class ResourceController
{
    public function __call()
    {
        // Call the authorization
        $this->authorizeForMethod($method);

        // We call the intended method. If it does not exist it should throw an
        // non existing method call exception like PHP does by default we do
        // not want to change this behaviour.
        $result = $this->$method(...);

        // We check if the $result is of instance response. If so we return this.
        if ($result instanceof Response) {
            return $result;
        }

        // Otherwise we will make a response with the results.
        else {
            // Depening on the method is being called we return different responses.
            return $this->respondForMethod($method, $results);
        }
    }
}

This change will result in less duplicate code in our controllers and keeps the possibility of returning a custom response.

@thomasowow
Copy link
Member Author

Is fixed in #11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant