How to handle webhook in Laravel
Hello, developers how’s life going. Today’s topic is about How to handle webhook in Laravel.
What is laravel
Laravel is an open-source PHP framework. which follows the Model-View-Controller (MVC) architectural pattern. it is developed by Taylor Otwell. The first release of laravel is in June 2011. If you want to know more about laravel you can go to its documentation from here.
What is Webhook
The webhook is changing the behavior of the application or web page using a callback. the callback can be created and managed by any developer. for example, you can change the messaging behavior of the LINE application using webhooks with your custom callback. If you want to know more about webhooks read from here.
The motive of this article is to handling those Webhook with laravel. if you want to learn more about LINE messaging API and custom Line Chatbot with laravel then will create another article just let me know in the comment section.
Step One create a laravel project
In this article, I am assuming that you already have some little knowledge about Laravel.
Run the following command in your terminal.
If you already have a laravel installer then run following command.
laravel new handlewebhook
If you did not have a laravel installer then run following command.
composer create-project --prefer-dist laravel/laravel handlewebhook
Step Two Create a Controller and Method
After you create a laravel project then create a controller webhook by running following command.
php artisan make:controller WebhookController
create a method called webhooks (you can use any name you want). Now write your logic there.
public function webhook(Request $request){ $data = $request->all() // change YOURMODEL with your model name $webhookmodel = YOURMODEL::create([ 'receiveData' =>$data ]); // you can return response from here }
Step Three Create a Route.
this is a very important part of this article make a route with the POST method yes post method.
Route::post('/webhook','WebhookController@webhook');
Now use this URL as your webhook URL on any web page or web application then check what the data you have received by calling your model list or by checking the database table. now you have an idea about how to handle webhook in laravel.
if you want to know more about REST API development with laravel then visit the site frequently.