I
I
Ilyas Masirov2015-02-16 18:07:35
Laravel
Ilyas Masirov, 2015-02-16 18:07:35

How to make life easier when working with MVC?

Hello people (robots, ufo)!
I'll go straight to the question. There is a News model and a NewsController controller. The controller has CRUD methods (create, show, edit, destroy) and some others. There is a master template from which other templates are inherited and certain data from the controller is displayed there. This master template has a sidebar that displays sections of the News category - All news, industry news, other news. So, every time I have to write requests to get these sections in the controller methods - $news_category = NewsCategory::all() and stuff them into CRUD templates. How to get rid of this routine?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
andreyqin, 2015-02-17
@Ilyas Masirov

Use View::share()in a class constructor to access a variable from any template.

V
Vyacheslav Plisko, 2015-02-16
@AmdY

You can use laravel.com/docs/4.2/responses#view-composers
You can use filters.
You can extend the base controller and pull the desired method.
And other options....

R
raiboon, 2015-02-16
@raiboon

Php can't get into normal mixins, which means traits.

A
Alexander Aksentiev, 2015-02-16
@Sanasol

Before rendering the template, pass global variables to it.
https://gist.github.com/S-anasol/fd287d8b4d2255f998c7

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question