在 Laravel 中放置菜单逻辑的位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13542688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:26:02  来源:igfitidea点击:

Where to place menu logic in Laravel?

phpmenularavel

提问by Orbitum

What is best conceptual place to put menu data logic in Laravel. If I use Menu bundle where to put it. In Base_Controllercreate additional function or something different?

在 Laravel 中放置菜单数据逻辑的最佳概念位置是什么。如果我使用 Menu bundle 把它放在哪里。在Base_Controller创建附加功能或不同的东西?

回答by akhy

Note: this answer was written for Laravel 3 and might or might not work with the most recent Laravel 4

注意:此答案是为 Laravel 3 编写的,可能适用于最新的 Laravel 4,也可能不适用



My favorite way of creating dynamic menu is achieved by separating the menu part from main layout and injecting the menu data via Laravel's Composer (don't confuse it with Composer PHP package manager, they are different things)

我最喜欢的创建动态菜单的方法是通过将菜单部分与主布局分离并通过 Laravel 的 Composer 注入菜单数据来实现的(不要将它与 Composer PHP 包管理器混淆,它们是不同的东西)

<!-- layouts/default.blade.php -->

<div id="header">Title</div>

<div id="menu">
    @render('parts.menu')
</div>

<div id="content"></div>
<div id="footer"></div>

 

 

<!-- parts/menu.blade.php -->

<ul>
@foreach($menuitems as $menuitem)
    <li>{{ $menuitem->title }}</li>
@endforeach
</ul>

 

 

Finally we can inject the variable via composer.

最后我们可以通过 composer 注入变量。

<?php 

// application/routes.php

View::composer('parts.menu', function($view){
    $view->with('menuitems', Menu::all());
});

This way everytime parts/menu.blade.phpis called, Composer will intercept the view and inject it with $menuitemsvariable. It's same as using withon return View::make('blahblah')->with( 'menuitems', Menu::all() )

这样每次parts/menu.blade.php调用时,Composer 都会拦截视图并将其注入$menuitems变量。这与使用withreturn View::make('blahblah')->with( 'menuitems', Menu::all() )

Hope it helps :)

希望能帮助到你 :)



Edit: If you don't like to have logics in routes.phpyou can put it in start.phpand consider Jason Lewis' way of splitting the start.phpinto separate files.

编辑:如果你不喜欢有逻辑,routes.php你可以把它放进去,start.php并考虑杰森刘易斯将它们start.php分成单独文件的方式。

Create a directory in applicationcalled startand fill it with some files.

application调用中创建一个目录start并用一些文件填充它。

    + application [DIR]
    \-> + start [DIR]
        |-> autoloading.php
        |-> composers.php
        |-> filters.php
        \-> validation.php

Then add these lines of code into the end of your application/start.php

然后将这些代码行添加到您的末尾 application/start.php

require __DIR__ . DS . 'start' . DS . 'autoloading.php';
require __DIR__ . DS . 'start' . DS . 'filters.php';
require __DIR__ . DS . 'start' . DS . 'composers.php';
require __DIR__ . DS . 'start' . DS . 'validation.php';

You got the idea. Put the composer functions in composers.php.

你明白了。将 Composer 函数放在 composers.php 中。

Read the entire article here: http://jasonlewis.me/article/laravel-keeping-things-organized

在此处阅读整篇文章:http: //jasonlewis.me/article/laravel-keeping-things-organized

回答by Franz

How about fetching the data in a view composerand using a HTML macrofor generating the HTML?

如何在视图编辑器中获取数据并使用HTML 宏来生成 HTML?

Laravel often has many ways of doing things. That said, this can probably be a little overwhelming and confusing at times.

Laravel 通常有多种做事方式。也就是说,这有时可能会有点让人不知所措和令人困惑。