Giter Site home page Giter Site logo

dheia / laravel-actions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lorisleiva/laravel-actions

1.0 0.0 0.0 607 KB

⚡️ Laravel components that take care of one specific task

Home Page: https://laravelactions.com

License: MIT License

PHP 100.00%

laravel-actions's Introduction

⚡️ Laravel Actions

Latest Version on Packagist GitHub Tests Action Status Total Downloads

hero

Classes that take care of one specific task.

This package introduces a new way of organising the logic of your Laravel applications by focusing on the actions your application provide.

Instead of creating controllers, jobs, listeners and so on, it allows you to create a PHP class that handles a specific task and run that class as anything you want.

Therefore it encourages you to switch your focus from:

"What controllers do I need?", "should I make a FormRequest for this?", "should this run asynchronously in a job instead?", ect.

to:

"What does my application actually do?"

Installation

composer require lorisleiva/laravel-actions

Documentation

📚 Read the full documentation at laravelactions.com

Basic usage

Create your first action using php artisan make:action PublishANewArticle and define the asX methods when you want your action to be running as X. E.g. asController, asJob, asListener and/or asCommand.

class PublishANewArticle
{
    use AsAction;

    public function handle(User $author, string $title, string $body): Article
    {
        return $author->articles()->create([
            'title' => $title,
            'body' => $body,
        ]);
    }

    public function asController(Request $request): ArticleResource
    {
        $article = $this->handle(
            $request->user(),
            $request->get('title'),
            $request->get('body'),
        );

        return new ArticleResource($article);
    }

    public function asListener(NewProductReleased $event): void
    {
        $this->handle(
            $event->product->manager,
            $event->product->name . ' Released!',
            $event->product->description,
        );
    }
}

As an object

Now, you can run your action as an object by using the run method like so:

PublishANewArticle::run($author, 'My title', 'My content');

As a controller

Simply register your action as an invokable controller in a routes file.

Route::post('articles', PublishANewArticle::class)->middleware('auth');

As a listener

Simply register your action as a listener of the NewProductReleased event.

Event::listen(NewProductReleased::class, PublishANewArticle::class);

Then, the asListener method of your action will be called whenever the NewProductReleased event is dispatched.

event(new NewProductReleased($manager, 'Product title', 'Product description'));

And more...

On top of running your actions as objects, controllers and listeners, Laravel Actions also supports jobs, commands and even mocking your actions in tests.

📚 Check out the full documentation to learn everything that Laravel Actions has to offer.

laravel-actions's People

Contributors

lorisleiva avatar mortenscheel avatar 0xflotus avatar morrislaptop avatar wulfheart avatar authanram avatar d8vjork avatar nickfls avatar hivokas avatar jenky avatar mga599 avatar markvaneijk avatar phcostabh avatar

Stargazers

Dheia avatar

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.