Email: info@zenconix.com

Laravel – Create Common Response builder for all responses

Published on: 07/8/20 5:36 PM

Category:Code Laravel PHP Tags: , , , , ,

Create Helper function

Create new folder in app/http/ by name Helpers

Create new file ResponseBuilder.php

Create new class in ReponseBuilder.php with function with same name as file, so class name will be ResponseBuilder

first we need to add namespace, as our file is in app\http\Helpers folder, namespace for the file would be namespace App\Http\Helpers;

<?php
    namespace App\Http\Helpers;

    class ResponseBuilder
    {
        public static function Result($status="", $information="", $data="")
        {
            return [
                "success"=>$status,
                "information"=>$information,
                "data"=>$data
            ];
        }
    }
?>

Note: here we have created static function, so later we can directly use it without initialization of object using :: operator.

Call Helper function in GET method

public function GetAllPost()
{
        //return Post::all();
        $data = Post::all();
        $status = true;
        $information= "Data is fetched successfully";

        return ResponseBuilder::Result($status, $information, $data)
}

Call Helper function in POST Method

public function AddPost(Request $request)
    {
        $post = new Post;

        $post->id = $request->input('id');
        $post->title = $request->input('title');
        $post->content = $request ->input('content');
        //$post->save();
        $result = $post->save();
        if($result == 1)
        {
            $status = true;
            $information = "Data added successfully";
        }
        else
        {
            $status = false;
            $information ="Data not added successfully";
        }
        return ResponseBuilder::Result($status, $information);
    }

Create Routes

//add post
$router->post('/addpost', 'PostController@AddPost');
//get all posts
$router->get('/getallpost','PostController@GetAllPost');

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

How Angular Application starts?

In this post we are going to take look on how angular application starts. For any angular application, entry point is index.html. In index.html file there is tag called as <app-root> In main.ts file <app-root> selector is picked up by the AppModule. Below is code snippet from main.ts file AppModule is module which is bootstrapped […]

Learn C# – Namespaces

What is Namespaces in C#? C# Programs are organized using namespaces. Namespaces are used to add separation of Code in C#. Namespaces can have following members inside it. Namespaces (nested) Classes Interface Delegates Structures Why to use Namespaces? Code Separation: With help of Namespaces, you can separate out set of code in C# Project.Example: if […]

Steps by step procedure to connect the database to Lumen

Create database in phpmyadmin Open .env file. NOTE: if .env file is with name, .env.sample, then rename it to .env Change following parameter in file DB_DATABASE=lumen DB_USERNAME=root DB_PASSWORD= Also check for following parameters if they are different than default setting DB_HOST=127.0.0.1 DB_PORT=3306 Next, as we are going to use Eloquent ORM, we need to un-comment […]