Created
February 4, 2021 16:04
-
-
Save MrTrick/d4b4599ae256b582584f9d2095deef48 to your computer and use it in GitHub Desktop.
Simple direct-injection of custom requests with validation rules.
This file contains hidden or 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 declare(strict_types=1); | |
namespace App\Providers; | |
use Illuminate\Contracts\Validation\ValidatesWhenResolved; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\ServiceProvider; | |
/** | |
* Class AppServiceProvider. | |
*/ | |
class AppServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Register any application services. | |
* | |
* @return void | |
*/ | |
public function register(): void | |
{ | |
} | |
/** | |
* Bootstrap the application events. | |
* | |
* @return void | |
*/ | |
public function boot(): void | |
{ | |
// Validate "validatable" injected objects | |
$this->app->afterResolving(ValidatesWhenResolved::class, function (ValidatesWhenResolved $resolved): void { | |
// Translate injected requests | |
if ($resolved instanceof Request) { | |
Request::createFrom($this->app->get('request'), $resolved); | |
} | |
$resolved->validateResolved(); | |
}); | |
} | |
} |
This file contains hidden or 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 declare(strict_types=1); | |
namespace App\Http\Controllers; | |
use App\Http\Requests\MyRequest; // Subclass of App\Http\Requests\Request | |
... | |
final class MyController extends Controller | |
{ | |
... | |
public function myAction(MyRequest $request): JsonResponse | |
{ | |
// The injected MyRequest object has already been populated, | |
// validated against what's returned by `$request->rules()` | |
// and property hinting like `$request->myfield` works. | |
... | |
} | |
... | |
} |
This file contains hidden or 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 declare(strict_types=1); | |
namespace App\Http\Requests; | |
use Illuminate\Http\Request as BaseRequest; | |
use Illuminate\Contracts\Validation\ValidatesWhenResolved; | |
use Illuminate\Contracts\Validation\Validator; | |
use Illuminate\Support\Facades\Validator as ValidatorFacade; | |
use Illuminate\Validation\ValidatesWhenResolvedTrait; | |
/** | |
* Common request class. | |
*/ | |
abstract class Request extends BaseRequest implements ValidatesWhenResolved | |
{ | |
use ValidatesWhenResolvedTrait; | |
/** | |
* Build a validator instance for the request. | |
* | |
* @return Validator | |
*/ | |
protected function getValidatorInstance(): Validator | |
{ | |
return ValidatorFacade::make($this->all(), $this->rules(), $this->messages()); | |
} | |
/** | |
* Return the validation rules that apply to this request. | |
* | |
* @return array | |
*/ | |
protected function rules(): array | |
{ | |
return []; | |
} | |
/** | |
* Return any custom error messages for validation of this request. | |
* | |
* @return array | |
*/ | |
protected function messages(): array | |
{ | |
return []; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment