PHP code example of sauvesolutions / presenters

1. Go to this page and download the library: Download sauvesolutions/presenters library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sauvesolutions / presenters example snippets


class ExampleUnpresenter extends SauveSolutions\presenters\Unpresenter {

    //specify any date fields
    protected $dates = ['a_date'];

    //specify any checkboxes that may be expected in the data sent with the web request
    protected $checkboxes = ['important'];

    /**
     * Specify the validation rules to be applied.
     *
     * @param $bUpdate boolean Pass true if the object is being updated, false otherwise.
     * @return array The validation rules.
     */
    public function getValidationRules($update) {
        //if updating an existing record then $update would be true, for a new record it would be false. This allows you
        //to create different rules for updates to existing records or creation of new records, e.g. unique checks that exclude the current record.
        //for this example however simply use a single response.

        return array(
                    'a_date' => 'date_format:d/m/Y'
                );

    }


}


public function store() {

    $input = Input::except('_token', '_method');

    $input = new ExampleUnpresenter($input);

    //simple try/catch block - the exception could be caught globally if preferred.
    try {
        //now try and validate
        $input->validate(false);
    } catch (SauveSolutions\exceptions\ValidationException $e) {
        //if it failed then redirect back.
        return Redirect::route('example.create')->withInput()->withErrors($e->getValidationErrors());
    }

    //now create a new example model.
    $model = new ExampleModel();
    $model->a_date = $input['a_date'];
    $model->save();

    //and finally let's show the index page.
    return Redirect::route('example.index');

}