What to do to call a function from another controller in Laravel? I would like to implement best practice

Asked

Viewed 3,143 times

1

I have a boletosController controller need to call the Function create in the tradeController;

boletosController.php;

 namespace App\Http\Controllers;

    use App\Repositorios\Boletos;  
    use Illuminate\Support\Facades\Request;

    class BoletosController extends Controller
    {
        protected $boletos;

        public function __construct(Boletos $boletos)
        {

            $this->boletos = $boletos;
        }


        public function create($data)
        {

            $create = $this->boletos->create($data);
            return $create;
        }

    }

How best to call the create function in the trading controller:

tradeController.php

namespace App\Http\Controllers;

    use App\Models\Boletos;
    use App\Models\Negotiation;


    class NegociacaoController extends Controller
    {

        public function index()
        {

         }


       public function gerarBoletosNegociacao()
        {

         //chamar aqui uma função de outro controller


         }

  • 3

    The best way is not to call. If your controller has a greater responsibility than just handling the http request, then there is something wrong. Logic should be implemented in another structure, usually called service.

  • For example, why not call the method create from your repository instead of calling from the controller?

  • Then in that case I would have to reverse, call the Trading model and deal within the controller that generates the billets?

  • As already mentioned here in the comments, apparently there is something wrong there, the correct thing is you create a Service and put this function in it, and then you "inject" as dependency the service in the 2 controllers.

1 answer

2

I agree with @Andersoncarloswoss positioning. You should implement using another structure. I will extend his argument; the controllers in MVC can be a kind of service or Façade.. because they can orchestrate calls to other logics and structures, such as database access and business rules themselves.

What happens is that many people make business rules directly in the controller. But then you ask me, isn’t the controller the place for rules? Yes, interface response rules. Not necessarily business rules or data access rules. It may be that in simple structures, you call the database directly from there... but in more robust or more elaborate, more complex software, you will call your classes and your business rules from there and not create them there inside the controller.

This server for Larvel and any other technology, we are talking about component architecture.

Recommend:

  1. Cohesion and Coupling, to understand principles of responsibility https://www.devmedia.com.br/entendendo-coesao-e-acoplamento/18538

  2. A structural Pattern: Facade. to try to share the view that the mvc controller can be a method that does not necessarily concentrate business rules. https://www.dofactory.com/net/facade-design-pattern

  3. A bit of component-oriented architecture, for you to use the principles of responsibility creating components that can be invoked at different times and even architectures: https://en.wikipedia.org/wiki/Component-based_software_engineering

Note about the model: In addition, the boy there asked you to do it directly at Model. This is not legal, as the model in MVC can be a representation of its database structure. This is common to learn there, but it is not an absolute truth, as it is more to be a representation of the data structure that will be presented than to be the data structure that is stored. Often the interfaces show information that has different data source, with its different structures.

Browser other questions tagged

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