0

I am designing a RESTful application and I would like to manage url parameters, at the moment I have this function in controller for the GET that list all the resources api/v1/cats:

public function index()
{
    $cats = Cats::all();

    foreach ($cats as $cat) {
        $requirement->view_requirement = [
                'href' => 'api/v1/cat/' . $cat->id,
                'method' => 'GET'
        ];
    }

    $response = [
            'msg' => 'List of all Cats',
            'cats' => $cats
    ];
    return response()->json($response, 200);
}

and route is :

Route::group(['prefix' => 'v1'], function() {

Route::resource('cats', 'CatController', [
        'except' => ['edit', 'create']
]);

which is the best way to manage url with for example search parameter like: api/v1/cats?name=Filip&color=black

2 Answers 2

1

you should not add any thing to the route file to handle request params just catch them in the controller as \Input::all()

then you can search and retrieve the result.

any you can use this to handle search on model level

https://github.com/nicolaslopezj/searchable

Sign up to request clarification or add additional context in comments.

Comments

1

To retrieve GET parameters you can do both ways :

  • TypeHint the Request class (global namespace) to inject the Request object and get your parameter via $request->get('filter')
  • Use the request()helper function this way request()->get('filter') or the shortcut 'request('filter')

Little tips about REST APIs : I don't know the stage of development of your project, but there are some guidelines / best practises for REST APIs and I highly encourage you to follow them. It will guide and help you making an awesome, robust, maintenable API. Speaking of experiences ;)

Here is an example : http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api

EDIT : You can of course still use $_GET

2 Comments

There is also this way that I like : $request->filter
and request()->filter too ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.