vendredi 29 avril 2016

Common method for Android and Website : Laravel 5.2

I have following method in controller.

private function SaveChanges(\App\Http\Requests\CountryRequest $request) {
    $Object = array(
        'Country'     =>  $request['Country'],
        'CountryCode' =>  $request['CountryCode'],
        'CountryID'   =>  $request['CountryID']
    );
    if($request['CountryID'] == 0) {
        $result = (new \App\DataAccess\CData())->CreateCountry(json_encode( $CObject ));
    }
    return redirect()->route($this->AllCountries);
}

Below is the Request class

class CountryRequest extends Request
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'Country'       => 'required|max:25|min:5|unique:tblcountry,Country,'. 
                                   \Request::get('CountryID') . ',CountryID',
            'CountryCode'   => 'required|max:5|min:2|unique:tblcountry,CountryCode,'.
                                   \Request::get('CountryID') . ',CountryID',
        ];
    }
}

Below is the method that will save the record in database.

class CountryData {

    public function CreateCountry($CountryObject) {
        $obj = json_decode($CountryObject);
        $Country = new \App\Models\CountryModel();
        $Country->Country       = $obj->Country;
        $Country->CountryCode   = $obj->CountryCode;
        $Country->save();
        return true;
    }
}

I have confusion in the flow. Below are my confusions.

I have to develop a website and an Android App. Android App will call Laravel method to fetch data. JSON data will be received and send to Laravel through Android. In order to achieve this, I removed save code from Controller action method and placed in a class called CountryData as given above.

I am assuming that there will be two routes to save the data.

  1. For Android App to return JSON result after save.
  2. For Website to redirect to a View after save.

What's the confusion?

I have Request class in Controller Action method to make sure max and min char validation and Duplicate check. But this is being done in the Controller Action method and will work for the website.

Android App will also need above validation. So Do I need validation at two places?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire