0

I've got 2 classes in my application both have their own Controller. Week -> WeekController Day -> DayController

When a new week is created I also want to create 5 (work)days for that week.

At this point I make a call from the create method in the WeekController to the create method in the DayController:

app('App\Http\Controllers\DayContoller')->create($week->id);

This post mentions this as being a bad practice. The alternatives offered however don't seem better in my case: A week doesn't really extend a day and using a trait would feel strange to me in this scenario as well.

So I wonder, what's the best solution in this scenario. I'm pretty sure this is something that's pretty common.

UPDATE: Not quite sure if this is better but I already rewrote as follows: I'm now injecting the DayController in the create method of the WeekController.

create(DayController $dayController, $id)

UPDATE2: I wrote a closure function in my route where I call a new written service.

function(CreateWeek $createWeek, $id) 
{
    $createWeek->createWeekWithDays($id);
} ]);

In this service I have a constructor that instantiates my 2 Controllers

public function __construct(WeekController $weekController, DayController $dayController)
{
    $this->weekController = $weekController;
    $this->dayController = $dayController;
}

and finally the createWeekWithDays method:

public function createWeekWithDays($id)
{
    //Create the week
    $newId = $this->weekController->create($id);

    // Create the days
    $this->dayController->createDays($newId);  
}

I don't know what's the best solution...

Lloople
  • 1,827
  • 1
  • 17
  • 31
DiscoFever
  • 123
  • 8
  • I think the post you linked to points to calling other controllers bad practice. I imagine you have models for `Week` and `Day`. Why not just create a week and 5 days in the Week controller? – Pawel Bieszczad Aug 24 '16 at 17:27
  • That would be an option as well. However I wanted to reuse the logic in the DayController's create method instead of replicating it. – DiscoFever Aug 24 '16 at 20:30
  • Then extract that logic to another class (service maybe) and use it both in day controller and week controller – Pawel Bieszczad Aug 24 '16 at 20:37
  • At this point I already rewrote my code, now injecting the DayController in the create method of the WeekController. `create(DayController $dayController, $id)` – DiscoFever Aug 24 '16 at 21:49
  • @PawelBieszczad I was actually trying to do that but at this point I don't manage how to get those service classes to work. As far as I understand it's best to store as much business logic as possible in those classes but I don't see where they fit in the complete picture. My route calls a controller and in that controller I bind/inject the service class, is that how it's supposed to be done? – DiscoFever Aug 24 '16 at 21:56
  • Injecting controller is not how you want to do this. Can show the code for both `createDays` and `createWeek` method from your controllers? Are you using models? – Pawel Bieszczad Aug 24 '16 at 23:04
  • Yes, I'm using models. Here's the createWeek method: `public function createWeek($id) { //Create the week $newWeek = new Week(); $newWeek->id = ''; // Use AutoIncrement value of MySql $newWeek->weekType ='U'; $newWeek->created_at = Carbon::now(); $newWeek->save(); Debugbar::info($newWeek); return $newWeek->id; }` The method for the days is similar except createDays loops 5 times over the createDay method. Not much logic in here right now but the logic still needs to grow. – DiscoFever Aug 25 '16 at 08:13

0 Answers0