lundi 11 juillet 2022

Splitting methods between controllers

I currently have a controller that is beginning to grow out of control. I'm now going to split it into separate controllers to make it easier to manage and maintain. This is legacy code and therefore, unfortunately, is running laravel 5.2. Upgrading laravel and php version is not an option at the current stage.

Is the following approach ok? Or is there a better way of doing this?

Current Approach

class StoreDistributionData extends Controller
{
    public function method1()
    {
        // code
    }

    public function method2()
    {
        $array = [];
        // lots of logic building above array

        $this->method3($array);
    }

    public function method3($array)
    {
        // code relating to above array
    }

    public function method4($array)
    {
        foreach($array as $k => $v) {
            // more logic
        }
        // more logic using various methods
    }

    // more methods and code
}

Revised Approach

class FormatDistributionData extends Controller
{
    public function method1()
    {
        // code
    }

    public function method2()
    {
        $array = [];
        // lots of logic building above array

        app(StoreDistributionData::class)->splitData($array);
    }
}

class StoreDistributionData extends Controller
{
    public function splitData($array)
    {
        // code relating to above array processed/stored using internal methods
    }

    // more methods
}


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/GufQhYk
via IFTTT

Aucun commentaire:

Enregistrer un commentaire