我有下面的代码将国家信息保存在数据库中。下面的代码运行良好。这没有问题
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);
}
现在,我决定将上面方法的工作转移到下面这样的新类中。这里我正在读取JSON数据
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;
}
}
,原始功能更改如下。以JSON的形式发送Request参数
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);
}
问题:将转换后的请求对象发送到JSON对象并在另一个类中读取的方法正确吗。
我这样做是为了创建一个新的控制器,并从类CountryData中调用CreateCountry来返回Android应用程序的JSON数据
首先,您不应该对对象等进行任何转换。其次,由于请求对象应该是一个数组,如您的示例所示,我建议您使用Laravel的"fill"方法,而不是循环所有请求元素。
保存请求的代码应如下所示:
class CountryData {
public function CreateCountry($requestData) {
$Country = new 'App'Models'CountryModel();
$country->fill($requestData);
$Country->save();
return true;
}
}
"fill"方法循环所有数组键,并尝试将它们设置到对象实例中(如果对象实例具有这些键作为属性)。如果有任何额外的字段,它们会被修剪,你不会得到任何错误。干杯!:)
嗯,我认为这不是一个好方法。您的CountryData类充当服务,所以我认为它不必了解JSON,JSON是您的业务逻辑和系统外部(Android应用程序、web界面等)之间接口的一部分。
您的新Controller可能会接收JSON对象并使用JSON对象进行应答,但它必须将接收到的JSON转换为您的业务类,然后将它们传递给服务,在这种情况下是CountryData(不过这不是一个好名字)。
所以逻辑应该是:
Controller:
- receive request data
- call service and save or whatever
- encode to JSON
- send the response in JSON format
因此,您的业务类对JSON一无所知。
提供了一个不完全的代码解决方案,但它缺乏错误管理和更多的工作要做。它基于Laravel 5的一些功能。此外,我不知道你是否在使用REST,或者你在做什么样的请求。。。
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;
}
}