6

I have a little problem. What data keep in controllers and what in models? I know in models keep whole logic of applications etc, but what's query and helpers functions? for example

Controller:

public function add(Request $request)
{
    $item = new Item()
    $item->name = $request->name;
    $item->save();

    $this->makeDirectory();
}

private function makeDirectory()
{
    //make a directory with photo this product
}

Where should I keep "makeDirecory" method in controller or models?

This is another situation when i would delete product and reference from another table.

public function delete(Items $id)
{
    $id->delete();

    $this->deleteProperties($id->properties); // $id->properties is a method from Items model with references to table Properties
}

private function deleteProperties(Properties $id)
{
    $id->delete();
}

Should I keep "deleteProperties" method in controller, Items model or Properties model? and invoke this method from this model?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Piffek
  • 89
  • 1
  • 7
  • How big is the application? How proficient are you in Laravel? – Kyslik Jan 05 '18 at 07:57
  • You know, I made shop and i do refactoring code. My proficient? I learn how work method/class from laravel (vendor directory). – Piffek Jan 05 '18 at 08:04

2 Answers2

1

You should keep methods like makeDirectory() in a service class and call it with:

$this->fileService->makeDirectory($directory);

You should keep data related logic in model classes or repository classes and use it in controller with:

$this->model->getSomeData();

You may also want to google "Fat models, skinny controllers".

Regarding helper functions, you should use these only when you really need one. For example, isAdmin() is a very handy global helper, but you should never create helpers like getAllUsers() or Helpers::getAllUsers()

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • I thought so, Thanks! If i had more reputation, I'd give point to you :) – Piffek Jan 05 '18 at 08:06
  • It does not really make sense for Laravel users to google "fat model, skinny controller", since Twatwell applies the name to the instances of [active record](https://martinfowler.com/eaaCatalog/activeRecord.html). Adding more logic into those would actually make things only worse. – tereško Jan 06 '18 at 09:23
1

I use controllers only to validate the incoming data and passing data to views.

I add another layer of classes that I call Departments. So, I have a department for profiles, artiles, info pages etc. Each department has its own namespace and a set of classes connected with the functionality.

Always think about SoC - separation of concerns. If you put a lot of logic into a controller, it will eventually get huge, hard to maintain and extend.

Example:

Controller:

public function addItem (Request $request, Item $item, ItemStorage 
  $itemStorage) {

    if ($item->verifyInput($request->all())) {
        $itemStorage->createItem ($item, $request->all());
    }
    else {
        // ... handle input error
    }

    // ... view
}

App\Departments\Items:

class ItemStorage {

    public function createItem ($newItem, $attributes) {
        $newItem->create($attributes);
        // ...  prepare data for creating a directory
        $this->makeDirectory($directoryName);
    }

    private function makeDirectory ($directoryName) {
        //... create directory
    }
}

You can/should separate the tasks even further. ItemStorage might not need to handle actual directory creation. You can call another department/service class name e.g. DiskManagement. This department would contain Classes like FileSystem. So, inside the makeDirectory() method, you would call a method from a class specialized in file system operations.

Peter Matisko
  • 2,113
  • 1
  • 24
  • 47
  • In this departments you have DI models class ? How they work? I can any examples? – Piffek Jan 05 '18 at 10:40
  • Let me put some example together for you. Models are models, they carry your data and connection to the DB. Departments are serivces that contain business logic – Peter Matisko Jan 05 '18 at 11:15
  • Ok, but what is difference between services and departments? – Piffek Jan 05 '18 at 11:54
  • Just my naming :) Services are probably better. The point is to put business logic outside controllers. But I would suggest also outside models. Laravel doesn't force you into more class layers, but it is good to have them and separate business logic. – Peter Matisko Jan 05 '18 at 11:55
  • @Piffek I use departments, because my project is larger. So, actually, my departments contain service classes, plus several other supporting classes. It really depends on how big the project is. Hope it helps a little! :) – Peter Matisko Jan 05 '18 at 16:20