The best way to create generic views that require a database

For example, we have a menu that will create a url, taking the bullets and title from the database. All pages require a menu. By skipping menu data from a controller taken from the database, I will have each controller repeating the same codes, which is not DRY. SO, how can I include these in the "layout" view without requiring each controller to pass menu data? If this is the simplest one please excuse me, I started laravel today.

+3


source to share


1 answer


You can use Traits. Define a trait class with the methods you need, and "only use them in the classes you need. Anything you define in BaseController will be available to anything that extends it that you don't need.

MenuTrait

trait MenuControls 
{
    public function createMenu()
    {

    }
}

      



Your class that needs the menu controls:

class INeedMenusController extends BaseController
{
    use MenuControls;

    public function doSomething()
    {
        $someVar = $this->createMenu();
    }
}

      

+1


source







All Articles