aurawindsurfing's avatar

Why is it bad idea to use Request::isJson instead of API route?

Hi,

Can someone explain to me in a nutshell why I should not be doing this in my existing laravel app?

I want to add json responses to use it in a native app. I have working controllers, routes etc.

Now not to rewrite the whole logic I figured that for a simple usage I can do just this:

if (\Request::isJson()) {
            return \Response::json(compact('articles', 'categories', 'colors', 'id'));
        } else {
            return view('homepage', compact('articles', 'categories', 'colors', 'id'));
        }

and get a nice json response that I will be using inside my iOS android app.

I have a feeling however that apart from obvious pagination, authentication and etc there is a much more to it.

What am I doing wrong please?

0 likes
4 replies
Snapey's avatar
Snapey
Best Answer
Level 122

API routes are segregated because it is expected to use different authentication methods and to not use session management.

If you want to use sessions then I would stick with web.php and if you want to use passport or token authentication (stateless requests) then use api.php

At least thats the way I understand it

1 like
aurawindsurfing's avatar

Cheers @Snapey! So in other words this is acceptable if only I consume this api, if however I will use it in clients apps then would be good idea to use authentication and proper api routes.

Snapey's avatar

Only you can make that decision. You do need to be sure that your service is used appropriately and securely (and not just adopt security by obscurity)

1 like

Please or to participate in this conversation.