Press "Enter" to skip to content

Having json support on your existing laravel web application

Notice: Undefined index: HTTP_USER_AGENT in /var/www/html/laravelnepal/wp-content/plugins/crayon-syntax-highlighter/util/crayon_util.class.php on line 793

Notice: Undefined index: HTTP_USER_AGENT in /var/www/html/laravelnepal/wp-content/plugins/crayon-syntax-highlighter/util/crayon_util.class.php on line 793

Sharing is caring!

Having an JSON support is application is not a new thing anymore.

If you have well developed application it should not be that hard to add the JSON support.

Following are the steps to have JSON support in your application:

  1. Every request made to the application through JSON must have application/json as Accept header. Most of the libraries like vue-resource or axios have this implemented.
  2. Disable your VerifyCsrfToken MIddleware. You can do it in app/Http/Kernel.php.

  3. Are each return of your controller, do this
  4. Of course, response()->json() needs appropriate status code and response content.
  5. It means if request wants response to be in json, we should respond in json other wise it can either be view or redirect depending upon your condition on what needs to be done.

Now, you need to make login system. To do this, use Json Web Token. What it does basically is, it returns a token based upon the credentials your provided which has encrypted information about the user.

Therefore, next time you make an request to the application with this token, application will be easily able to recognize you.

This is just a trick, if you are trying to convert your old applications to JSON support.

For new application, I strongly recommend to use dingo/api package with laravel.

Everything is awesome.

Be First to Comment

    Leave a Reply

    Your email address will not be published.