logeeks
logeeks

Reputation: 4979

Laravel 5.x - Writing response failed when called from parent class

I have created the protected function manageresponse($success, $errorcode) method in the Controller class.

<?php

namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesResources;

class Controller extends BaseController
{
    private $responses = array(
        EMAIL_IN_USE        => 'Email is already in use.Please try with a different email.',
        INVALID_TOKEN       => 'Session expired. Please login to continue.',
        UNSUCCESSFUL_LOGIN  => 'Login unsuccessful.',
    );

    protected function manageresponse($success, $errorcode)
    {
        return response()-> json([
            'success' => $success, 'error' =>  $this->responses[$errorcode] , 'errorcode' => $errorcode
        ]);    
    }
}

My intention is to write all error responses from one common place. In the CardsController which is a child of Controller. I am accessing manageresponse like below

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Config;
use Log as EventLogger;

class CardsController extends Controller 
{
    public function transferEx($eventid=0, $pin)
    { 
        $this->manageresponse(SUCCESS, INVALID_TOKEN);
    }
}

I have noticed that response is not getting written at all. The execution just gets passed to next line.

Please help me in identifying the problem.

Thank you in advance.

Upvotes: 1

Views: 141

Answers (2)

Jaymin Panchal
Jaymin Panchal

Reputation: 2856

I had the same issue so and I want the same thing to generalize the response so I have created trait to manage the response.

ApiResponse.php in App/Common

use Illuminate\Http\Response;

trait ApiResponse
{

    protected $response = array();
    protected $meta = array();
    protected $data = array();

    protected function setMeta($code, $message = "")
    {
       $this->meta['code'] = $code;
       if ($message == null || $message == "") {
        switch ($code) {
            case "200":
                $message = "Fetched Successfully";
                break;
            case "422":
                $message = "Missing Parameters";
                break;
            case "404":
                $message = "Record not found";
                break;
            default:
                $message = "Invalid Response";
                break;
          }
       }
       $this->meta['message'] = $message;
    }

    protected function setData($key, $value){
        $this->data[$key] = $value;
    }

    protected function setResponse()
    {
       $this->response['meta'] = $this->meta;
       if (count($this->data) > 0) {
         $this->response['data'] = $this->data;
       }
       return $this->response;
    }
}

In Controller:

class CardsController extends Controller {

    use ApiResponse;

    public function transferEx($eventid=0, $pin) { 
       $this->setMeta("200"); //or $this->setMeta("200",$message);
       return response()->json($this->setResponse());           
   }

}    

Upvotes: 1

Daan Meijer
Daan Meijer

Reputation: 1348

I think you are missing a return statement in your controller function. You can fix it by using this controller code:

class CardsController extends Controller {

    public function transferEx($eventid=0, $pin) { 

        return $this->manageresponse(SUCCESS, INVALID_TOKEN );

    }

}

Upvotes: 0

Related Questions