Coder Social home page Coder Social logo

request-controller's Introduction

Use FormRequests as invokable controllers

Latest Version on Packagist GitHub Tests Action Status Total Downloads

Aren't you tired of having to create a FormRequest class for almost every invokable Controller you create?

It turns out, with a few tweaks, you can use a FormRequest class as a controller.

This package provides only one class: a RequestController class that extends the FormRequest class we all know and adapt it slightly so it works as an invokable Controller.

Installation

composer require lorisleiva/request-controller

Usage

class MyController extends RequestController
{
    public function middleware()
    {
        return [];
    }

    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [];
    }

    public function __invoke()
    {
        // ...
    }
}

Note that the middleware, authorize and rules methods are all optional and default to the value displayed in the example.

Since RequestController extends FormRequest, you have access to all the methods you are used to, like:

  • $this->get($attribute) — To access a request attribute.
  • $this->route($attribute) — To access a route parameter.
  • $this->validated() — To access the validated data.
  • $this->user() — To access the user.
  • Etc.

Similarly, you can override the same FormRequest methods you are used to, in order to customize the validation logic:

  • attributes() — To provide user-friendly names to your attributes.
  • message() — To customize your validation messages.
  • withValidator() — To extends the current validator.
  • validator() — To take full control over the validator created.
  • Etc.

Enjoy! ✨

request-controller's People

Contributors

lorisleiva avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

request-controller's Issues

Is DispatchesJob needed?

Firstly, this seems like a really useful little package!

Just want to question the reason behind using the DispatchesJob trait? i don't think its used.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.