You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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:
A suggestion would be to implement the
__call
method to allow a nonResponse
type to be returned by all resource methods. The following pseudo example could make things a little more clearer:This change will result in less duplicate code in our controllers and keeps the possibility of returning a custom response.
The text was updated successfully, but these errors were encountered: