在Laravel中何处放置菜单逻辑? [英] Where to place menu logic in Laravel?

查看:91
本文介绍了在Laravel中何处放置菜单逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel中放置菜单数据逻辑的最佳概念位置是什么.如果我使用Menu捆绑包放在哪里.在Base_Controller中创建其他功能还是其他功能?

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

推荐答案

注意:此答案是为Laravel 3编写的,可能与最新的Laravel 4无关,也可能不起作用.

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


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


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)

<!-- 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>

 

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

Finally we can inject the variable via composer.

<?php 

// application/routes.php

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

通过这种方式,每次调用parts/menu.blade.php时,Composer都会拦截该视图并将其注入$menuitems变量.与在return View::make('blahblah')->with( 'menuitems', Menu::all() )

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

希望它会有所帮助:)

编辑:如果您不喜欢routes.php中的逻辑,可以将其放在start.php中,并考虑杰森·刘易斯(Jason Lewis)将start.php拆分为单独文件的方式.

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

application中创建一个名为start的目录,并在其中填充一些文件.

Create a directory in application called start and fill it with some files.

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

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

Then add these lines of code into the end of your 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';

您明白了.将composer函数放在composers.php中.

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

在此处阅读整篇文章: http://jasonlewis.me/article/laravel-东西保持井井有条

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

这篇关于在Laravel中何处放置菜单逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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