SOLID Principles within Laravel Controller

Asked

Viewed 155 times

0

Within SOLID principles, how to implement the first 2 principles ?

SRP and Open-Closed Principle.

My structure is simple, it has models within the app generated by Artisan, and simple controllers. An example of a method that I would like to refactor is the one that I’m going to pass down..

I can’t find any place in Laravel where I can put the classes, there’s some standard place ?

It receives an HTTP request via ajax and renders the result in the view..

As an example I will pass a private function , which simply makes different requests depending on the status researched, I created a special function for her, not to have to repeat several times during the code

private function recarregarAjaxPedidos($statusPesquisado,$codEmpresa)
    {
        $pedidos = [];
        if($statusPesquisado == "T")
        {
            $pedido = DB:table /* resto da query */ ->get()

        } else  if($statusPesquisado == "T") {
            $pedido = DB:table /* resto da query */ ->get()
        } else  if($statusPesquisado == "PR") {
            $pedido = DB:table /* resto da query */ ->get()
        } else {
            $pedido = DB:table /* resto da query */ ->get()
        }

        return $pedido;

    }
  • 1

    Isn’t it better to maintain simplicity and efficiency??? Type ai you can work on this private method with Builder?

  • 2

    Don’t even try to do that. Study the subject thoroughly, or at least the basics before you do so. What’s the point of complicating something you don’t even understand yet? Make it simple, when you need to do something else, then you do it, while studying the subject. His question shows that he does not even understand what SOLID or these principles, read somewhere that he is cool (and it’s not like that, he has problems) and wants to use it anyway, more or less like this: https://i.stack.Imgur.com/ofbhu.jpg. Do you have any specific doubt?

  • 2

    It’s the same in Laravel the staff complica ta everything ready just use ... I also think should be use if you know and also if you need.

  • 2

    The question I ask you is "what advantage you see in this?", there is no need, I heard so many people talking about Solid as if it were a magic solution to problems, but most are people who use because they choose or because they do not understand and another person said "That’s the most modern". In short, ask yourself, what advantage I will have over this and if there is an advantage the way would be to understand how it works. I’ll be honest, the use of frameworks is mostly an exaggeration. Most Fws have poor performance and generally you do not use even 10% of the features.

  • Can you do that I remember with Switch

  • @Maniero I think that if he reviews the good practices already solves the bullshit

  • Of course I won’t modify the project I’m working on, but I think of a way to work on it using Solid, would create a class for each extending if of an abstract class of the Rendering type resultadosabstract. ?

Show 2 more comments

1 answer

-1

A way you might be using that would even help in the performance of your system.

private function recarregarAjaxPedidos($statusPesquisado,$codEmpresa)
    {
        $pedidos = [];

        switch($statusPesquisado){
            case("T"):
             $pedido = DB:table /* resto da query */ ->get();
             break;
            case("PR"):
             $pedido = DB:table /* resto da query */ ->get();
             break;
            default:
              $pedido = DB:table /* resto da query */ ->get();
        }

        return $pedido;

    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.