2016-04-14 37 views
3

我有下面的代码将国家信息保存在数据库中。下面的代码工作正常。这没有问题。现在在Laravel 5.2中将请求对象转换为JSON

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数据。

+1

当你可以发送数组并跳过'json_encode'和'json_decode'时,看起来像是浪费时间。你有没有理由需要这个json? – user3158900

+0

@ user3158900:我这样做是为了创建一个新的控制器,并从类CountryData中调用CreateCountry来返回Android应用程序的JSON数据。 – Pankaj

回答

0

嗯,我不认为这是一个好方法。您的CountryData类充当服务,所以我认为它不需要知道任何关于JSON的东西,这是您业务逻辑和系统外部(Android应用程序,Web界面等)之间的接口的一部分。 )

您的新控制器可接收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; 
    } 
} 
+0

你能分享正确的代码吗? – Pankaj

+0

不,我只能分享一些可以开始工作的示例代码,实际上我不知道如何实现您的应用。 –

+0

请求保存国家也将由Android发送。所以在这种情况下,我想我需要将JSON数据发送给Laravel。我认为,Android不会对模型定义有所了解。所以这就是我应该有另一个方法,它将有一个参数来接受JSON数据?我在你的代码中看到,所有的方法都有模型参数? – Pankaj

0

你们第一个不应该对对象等进行任何转换。 其次,由于请求对象应该是一个数组,如你的例子所示,我建议你使用Laravel的“fill”方法,而不是循环所有的请求元素。

您保存的要求应该如下代码:

class CountryData { 
    public function CreateCountry($requestData) { 

     $Country = new \App\Models\CountryModel(); 
     $country->fill($requestData); 
     $Country->save(); 
     return true; 
    } 
} 

的“补”的方法循环所有的数组键,并尝试将其设置到对象实例,如果有这些键的属性。如果有任何额外的领域,他们被修剪,你不会得到任何错误。 干杯! :)