Created
August 12, 2017 23:38
-
-
Save rayonhunte/a2ea48775684d4824c4118e6d0d29848 to your computer and use it in GitHub Desktop.
Code Separation and Debugging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Log; | |
use Laravel\Lumen\Routing\Controller as BaseController; | |
class Controller extends BaseController | |
{ | |
// | |
//custom methods | |
public function updateSetData($snag, $status, $report) | |
{ | |
$snag['status'] = $status->status; | |
$snag['type'] = $report->snag_type_id; | |
$snag['pilot_id'] = $report->pilot_id; | |
$snag['aircraft_type_id'] = $report->aircraft_type_id; | |
$snag['aircraft_registration_id'] = $report->aircraft_registration_id; | |
$snag['snag_type_id'] = $report->snag_type_id; | |
$snag['status_id'] = $report->status_id; | |
$snag['snag_id']= $report->snag_id; | |
$snag['status'] = $report->status_id; | |
return $snag; | |
} | |
public static function toSnake_Case($obj) | |
{ | |
foreach ((array) $obj as $key => $value) { | |
if (strcmp(snake_case($key), $key) == 0) { | |
continue; | |
} | |
$obj[snake_case($key)] = $value; | |
unset($obj[$key]); | |
} | |
return $obj; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use Illuminate\Http\Response; | |
//use App\Validations\SnagValidation; | |
use App\Snag; | |
use App\SnagStatus; | |
use App\SnagReport; | |
use App\Util; | |
use Log; | |
class SnagController extends Controller | |
{ | |
protected $snag, $snagStatus, $snagReport, $saveValRules ,$updateValRules; //$snagValidation,$snagType; | |
/** | |
* Create a new controller instance. | |
*Request $request, $id | |
* @return void | |
*/ | |
public function __construct(Snag $snag, SnagStatus $snagStatus, SnagReport $snagReport) //SnagValidation $snagValidation) | |
{ | |
$this->snag = $snag; | |
$this->snagReport = $snagReport; | |
$this->snagStatus = $snagStatus; | |
$this->saveValRules = [ | |
'snag' => 'required|min:3', | |
'createdById' => 'required|integer|min:1', | |
'pilotId' => 'required|integer|min:1', | |
'aircraftTypeId' =>'required|integer|min:1', | |
'aircraftRegistrationId' =>'required|integer|min:1', | |
'snagTypeId' =>'required|integer|min:1', | |
'statusId' => 'required|integer|min:1', | |
]; | |
$this->updateValRules=[ | |
'snag'=>'required|min:3', | |
'createdById' =>'required|min:1', | |
'updatedById' => 'required|min:1' | |
]; | |
// $this->snagValidation = $snagValidation; | |
} | |
// show a list of snags | |
public function index() | |
{ | |
return $this->snag->latest()->get(); | |
} | |
// show a single snag | |
public function show($id) | |
{ | |
return $this->snag->find($id); | |
} | |
// add a snag | |
public function store(Request $request) | |
{ | |
// define validation | |
$this->validate($request, $this->saveValRules); | |
// get all request data | |
$inData = self::toSnake_Case($request->all()); | |
// init snag | |
$snag = $this->snag->create($inData); | |
// init snag type | |
//Log::info ($snag->id); | |
$inData['snag_id'] = $snag->id; | |
$report = $this->snagReport->create($inData); | |
//init snag report | |
$report->snag = $snag; | |
return response()->json(array('serverEvent'=> 'SAVE', 'data' => array($report))); | |
} | |
//update snag | |
public function update(Request $request, $snagId) | |
{ | |
//validate input | |
$this->validate($request, $this->updateValRules); | |
//get all input data | |
$allData = self::toSnake_Case($request->all()); | |
$snag = Snag::find($snagId); | |
$snag->update($allData); | |
return response()->json(array('serverEvent'=> 'UPDATE', 'data' => array($snag))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Best Practice
The Above is a code extract of Laravel Controllers, the main controller and an extended controller (SnagController.php).
This shows separation of concerns, on line 18 where I am declaring the protected common variables.
This is done so the global imported classes are not used directly in the overall controller which makes it easier to update code and to swap in different classes and make code changes without having to update the whole controller.
Also, I have declared some classes in the parent controller that can be used in all child controllers.
Please also note on line 69, the use of the Log class.
As I develop I will add and remove the logging, DD and print_r when needed.
I add the above In cases when I would like to make sure that I am passing in the right values from a form or if I want to check what is being returned from a method.