S
S
Sergey Khlopov2020-05-25 11:48:03
Laravel
Sergey Khlopov, 2020-05-25 11:48:03

How can the controller be simplified?

There is such a controller, please tell me how to make it cleaner?

class PageController extends Controller
{
    public function catalog($alias)
    {
        $arrayUrl = explode("/",$alias);
        $entity = Category::GetByAlias(Arr::last($arrayUrl))->firstOrFail();
        return view("web.page.entity.entity",[
            "entity" => $entity,
            "children" => $entity->children()->SelectMini()->Active()->get(),
            "products" => $entity->products()->Active()->SortImages()->get(),
            "breadcrumbs" => (new Breadcrumbs)->make($entity)
                ->prepend(["name" => "Каталог","url" => route("catalogs")],true)
                ->get()
        ]);
    }
    public function services($alias)
    {
        $arrayUrl = explode("/",$alias);
        $entity = Service::GetByAlias(Arr::last($arrayUrl))->firstOrFail();
        return view("web.page.entity.entity",[
            "entity" => $entity,
            "children" => $entity->children()->SelectMini()->Active()->get(),
            "products" => $entity->products()->Active()->SortImages()->get(),
            "breadcrumbs" => (new Breadcrumbs)->make($entity)
                ->prepend(["name" => "Услуги","url" => route("services")],true)
                ->get()
        ]);
    }
    public function product($alias, $id)
    {
        $product = Product::with("sizes","colors","country","materials")->SortImages()->findOrFail($id);
        return view("web.page.product.product",[
            "product" => $product,
            "breadcrumbs" => (new Breadcrumbs)->make($product)
                ->prepend(["name" => "Каталог","url" => route("catalogs")],true)
                ->get()
        ]);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2020-05-25
@Shlop

What does Laravel say? He says split into controllers. For ProductController products etc. Name methods index show update etc. The data array for the view can be obtained from its class, but you can leave it like that if there is not enough data in the controller. I would revise the logic of generating data and names. there are no difficult tasks, but the processes are incomprehensible, some kind of $arrayUrl alias entity, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question