Created
October 15, 2019 15:09
-
-
Save shakkurcwb/41397b79a21056abe9b055928aeafb01 to your computer and use it in GitHub Desktop.
Controller + ActionController for Laravel
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\Support\Facades\Validator; | |
use Illuminate\Http\Request; | |
use App\Http\Controllers\Controller; | |
abstract class ActionController extends Controller | |
{ | |
protected $request; | |
protected $rules = [], $messages = [], $attributes = []; | |
/** | |
* Create a new action controller instance. | |
* | |
* @param Request $request | |
* @return Void | |
*/ | |
public function __construct(Request $request) | |
{ | |
$this->request = $request; | |
} | |
/** | |
* Get attributes from a request. | |
* | |
* @return Array | |
*/ | |
public function getPayload(): Array | |
{ | |
return $this->request->except([ '_token', '_method' ]); | |
} | |
/** | |
* Set attributes. | |
* | |
* @param Array $attributes | |
* @return Void | |
*/ | |
public function setAttributes(Array $attributes = []): Void | |
{ | |
$this->attributes = $attributes; | |
} | |
/** | |
* Set validation rules. | |
* | |
* @param Array $rules | |
* @return Void | |
*/ | |
public function setValidationRules(Array $rules = []): Void | |
{ | |
$this->rules = $rules; | |
} | |
/** | |
* Set validation messages. | |
* | |
* @param Array $messages | |
* @return Void | |
*/ | |
public function setValidationMessages(Array $messages = []): Void | |
{ | |
$this->messages = $messages; | |
} | |
/** | |
* Get a validator for an incoming request. | |
* | |
* @return \Illuminate\Contracts\Validation\Validator | |
*/ | |
private function validator(): \Illuminate\Contracts\Validation\Validator | |
{ | |
return Validator::make($this->attributes, $this->rules, [], $this->messages); | |
} | |
/** | |
* Validate payload and return attributes or fail with error. | |
* | |
* @return Array | |
*/ | |
public function validateOrFail(): Array | |
{ | |
return $this->validator()->validate(); | |
} | |
} |
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\Foundation\Bus\DispatchesJobs; | |
use Illuminate\Foundation\Validation\ValidatesRequests; | |
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; | |
use Illuminate\Routing\Controller as BaseController; | |
abstract class Controller extends BaseController | |
{ | |
use AuthorizesRequests, DispatchesJobs, ValidatesRequests; | |
/** | |
* Redirect a response. | |
* | |
* @param String $uri | |
* @return Illuminate\Http\RedirectResponse | |
*/ | |
public function redirect(string $uri): \Illuminate\Http\RedirectResponse | |
{ | |
return redirect($uri); | |
} | |
/** | |
* View response + data. | |
* | |
* @param String $slug | |
* @param Array $data | |
* @return Illuminate\Http\Response | |
*/ | |
public function view(String $slug = "", Array $data = []): \Illuminate\Http\Response | |
{ | |
return response()->view($slug, $data); | |
} | |
/** | |
* Json Response. | |
* | |
* @param Array $data | |
* @param Int $status | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
public function json(Array $data = [], Int $status = 200): \Illuminate\Http\JsonResponse | |
{ | |
return response()->json($data, $status); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment