about uscontact

How to create action class using artisan command in Laravel

Category:
Author: zemna
Date: August 20, 2022
Share this:

Today action class is populary used to manage code per each controller execution (like index, store, update, and destroy). Let's create action class more easily by making new php artisan command.

Create MakeActionCommand class

php artisan make:command MakeActionCommand

Update code in App\Console\Command\MakeActionCommand.php file like below:

<?php
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
class MakeActionCommand extends GeneratorCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'make:action {name}';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new action class';
    /**
     * The type of class being generated.
     *
     * @var string
     */
    protected $type = 'Action';
    /**
     * Determine if the class already exists.
     *
     * @param string $rawName
     * @return bool
     */
    protected function alreadyExists($rawName)
    {
        return class_exists($rawName);
    }
    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {
        return __DIR__ . '/stubs/action.stub';
    }
    /**
     * Get the default namespace for the class.
     *
     * @param  string  $rootNamespace
     * @return string
     */
    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace . '\Actions';
    }
}

Create action.stub file

Create action.stub file in App\Console\Commands\stubs\action.stub location and write code like below:

<?php
namespace DummyNamespace;
class DummyClass
{
    /**
    * Create the action.
    *
    * @return void
    */
    public function __construct()
    {
        //
    }
    /**
    * Execute the action.
    *
    * @return mixed
    */
    public function execute()
    {
        //
    }
}

Make your action class using command

Now, you can create your action class using php artisan command.

Let's make GetUsersAction class.

php artisan make:action GetUsersAction

Above command make class file in App\Actions\GetUsersAction.php location like below code:

<?php
namespace App\Actions;
class GetUsersAction
{
    /**
    * Create the action.
    *
    * @return void
    */
    public function __construct()
    {
        //
    }
    /**
    * Execute the action.
    *
    * @return mixed
    */
    public function execute()
    {
        //
    }
}
Share this:

Leave a Reply

Your email address will not be published.

ZEMNA.NET

Professional software development agency estalished in Indonesia. We offers website setup, web application development, fulfillment service, and etc.
Follow us
Follow us to get latest news and tutorials.
2022 - Copyright, All Rights Reserved, Made by ZEMNA.NET with ❤️
magnifiercrossmenu