I have below code that save the country information in Database. Below code works fine. There is no problem in that.
private function SaveChanges(\App\Http\Requests\CountryRequest $request) { if($request['CountryID'] == 0) { $Country = new \App\Models\CountryModel(); } else { $Country = $this->GetCountry($request['CountryID']); } $Country->Country = $request['Country']; $Country->CountryCode = $request['CountryCode']; $Country->save(); return redirect()->route($this->AllCountries); }
Now, I decided to shift the working of above method inside a new class like below. Here I am reading the JSON data
class CountryData { public function CreateCountry($CountryObject) { $obj = json_decode($CountryObject); $Country = new \App\Models\CountryModel(); $Country->Country = $CountryObject->Country; $Country->CountryCode = $CountryObject->CountryCode; $Country->save(); return true; } }
and the original function is changed like below. Sending the Request parameter in the form of JSON.
private function SaveChanges(\App\Http\Requests\CountryRequest $request) { $data = array( 'Country' => $request['Country'], 'CountryCode' => $request['CountryCode'], 'CountryID' => $request['CountryID'] ); if($request['CountryID'] == 0) { $result = (new \CountryData())->CreateCountry( json_encode($data) ); } return redirect()->route($this->AllCountries); }
Question: Is my approach correct to send converted request object to JSON object and reading in an another Class .
I am doing that so that I can create a new controller and call the CreateCountry from class CountryData to return JSON data for an Android App.
2 Answers
Answers 1
Well, I don't think it's a good approach. Your CountryData class acts as a service, so I think it hasn't have to know anything about JSON, that is part of the interface between your business logic and the external side of your system (Android app, web interface, etc.)
Your new Controller may receive JSON objects and answer with JSON objects, but it must convert the JSON received to your business classes, then pass them to your services, in this case CountryData (not a good name, though).
So the logic should be:
Controller: - receive request data - call service and save or whatever - encode to JSON - send the response in JSON format
So your business classes don't know anything about JSON.
A not fully code solution is provided as an idea, but it lacks error management, and more work to do. It's based on some Laravel 5 features. Also I don't know if you're using REST or what kind of request are you doing...
use App\Http\Controllers\Controller; class CountryController() extends Controller { public function store(\App\Http\Requests\CountryRequest $request) { // TODO manage errors $countryModel = $this->createOrUpdateCountry($request); // Laravel way to response as JSON return redirect()->json($this->country2Array($countryModel); } private function createOrUpdateCountry(\App\Http\Requests\CountryRequest $request) { $countryId = $request['CountryID']; if($id == 0) { $countryModel = new \App\Models\CountryModel(); } else { $countryModel = $this->GetCountry($countryId); } $countryModel->Country = $request['Country']; $countryModel->CountryCode = $request['CountryCode']; // You must have an initialised instance of CountryDAO // TODO manage errors $countryDAO->saveOrUpdate($countryModel); return $countryModel; } private function country2Array($countryModel) { $data = array( 'country' => $countryModel->Country, 'countryCode' => $countryModel->CountryCode, 'countryId' => $countryModel->CountryID ); return $data; } } /** * Formerly CountryData */ class CountryDAO { public function saveOrUpdate($countryModel) { // TODO Manage errors or DB exceptions // I'd put the DB save access/responsability here instead of in CountryModel $countryModel->save(); return true; } }
Answers 2
First of you should not do any conversions to objects and so on. Second, since the request object should be an array as shown on your example I suggest you to use the "fill" method of Laravel, instead of looping on hand all of the request elements.
Your code for saving the request should be as follows:
class CountryData { public function CreateCountry($requestData) { $Country = new \App\Models\CountryModel(); $country->fill($requestData); $Country->save(); return true; } }
The "fill" method loops all of the array keys and tries to set them into the object instance if it has those keys as properties. If there are any extra fields, they are trimmed and you wont get any errors. Cheers! :)
0 comments:
Post a Comment