数据库驱动的菜单,可以在视图中加载 [英] Database driven menu that can be loaded in view

查看:62
本文介绍了数据库驱动的菜单,可以在视图中加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用控制器创建了一个数据库驱动的菜单,

I have created a database driven menu using a Controller,

HomeController扩展了在Controller的构造函数中加载菜单的Controller.

HomeController extends Controller which the menu is loaded in Controller's construct function.

HomeController.php

HomeController.php

class HomeController extends Controller
{


    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth');
    }

    public function index(){
        $data['menu'] = $this->menu;
        return view('home', $data);
    }
}

Controller.php

Controller.php

public function __construct()
    {
        $this->user = Auth::user();

        $menu = new Menu();

        if($this->user != NULL && $this->user != ""){
            $this->menu = $menu->getMenu($this->user->user_id);
        }   
    }

我该如何直接在视图级别调用该函数,因为现在即使将菜单加载到构造函数中,我仍然需要将菜单传递给视图,这会使事情有些多余.

How can I, call the function straight at the view level because right now, even though the menu is loaded in the constructor, I will still need to pass the menu to the view which makes things a bit redundant.

P/S:使用laravel 5.1

P/S: Using laravel 5.1

推荐答案

通过遵循以下命令,从工匠那里生成新的ServiceProvider

Generate a new ServiceProvider from artisan by following command

php artisan make:provider ComposerServiceProvider

这将在app/Providers下创建一个新文件名ComposerServiceProvider.php.在这个新创建的服务提供者的启动功能中,您可以创建带有关闭功能的功能,如下所示:

this will create a new file name ComposerServiceProvider.php under app/Providers. In the boot function of this newly created service provider you can create functions with closure like following :

view()->composer('partials.navbar', function ($view) {
        $view->with('genre', Genre::all());
    });

这里讨论的视图是view/partials下的navbar.blade.php,它将在您的应用程序中提供一个名为genre的变量.

here the view in question is navbar.blade.php under view/partials which will have a variable named genre available through out your app.

要使代码更整洁,您可以做的是在ComposerServiceProvider中创建一个新函数,并命名为"partialnav".然后将执行以下操作:

To make your code cleaner what you can do is create a new function in the ComposerServiceProvider and name it anything lets say partialnav. Then will do the following :

public function boot()
{
    $this->partialNav();
}

//create a function independently

public function partialnav()
{
    //code goes here
}

如果您想进一步分离它,则可以在app/Http名称下创建一个名为ViewCompoers的新文件夹,在该文件夹下使用以下代码创建一个名为NavbarComposer.php的新文件:

If you want to separate it even more you can create a new folder under app/Http name it lets say ViewCompoers, Under this folder create a new file named NavbarComposer.php with the following code :

class NavbarComposer {


/**
 * Create a new profile composer.
 *
 * @param  UserRepository  $users
 * @return void
 */
public function __construct()
{
    // Dependencies automatically resolved by service container...
}

/**
 * Bind data to the view.
 *
 * @param  View  $view
 * @return void
 */
public function compose(View $view)
{
    //write your code to fetch the data 
    // and pass it to your views, following is an example
    $genre = genre::all();
    $view->with('genre', $genre);
}
}

现在回到您的ComposerServiceProvider的partialnav函数

now back to your ComposerServiceProvider's partialnav function

public function partialNav()
{
    view()->composer('partials.nav', 'App\Http\ViewComposers\NavbarComposer');
}

不要忘记在您的config/app.php中添加这个新创建的ServiceProvider

Don't forget to add this newly created ServiceProvider in your config/app.php

App\Providers\ComposerServiceProvider::class,

这篇关于数据库驱动的菜单,可以在视图中加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆