Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What add to controller and what to models

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?

like image 344
Piffek Avatar asked Jan 05 '18 07:01

Piffek


2 Answers

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()

like image 106
Alexey Mezenin Avatar answered Nov 18 '22 19:11

Alexey Mezenin


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.

like image 1
Peter Matisko Avatar answered Nov 18 '22 21:11

Peter Matisko