在 Laravel 中放置菜单逻辑的位置?
在 Laravel 中放置菜单数据逻辑的最佳概念位置是什么?如果我使用 Menu bundle 把它放在哪里.在 Base_Controller
中创建附加功能或不同的东西?
注意:此答案是为 Laravel 3 编写的,可能适用于最新的 Laravel 4,也可能不适用
<小时>
我最喜欢的动态菜单创建方式是将菜单部分与主布局分离,并通过 Laravel 的 Composer 注入菜单数据(不要将其与 Composer PHP 包管理器混淆,它们是不同的东西)
<div id="header">标题</div><div id="菜单">@render('parts.menu')<div id="内容"></div><div id="页脚"></div>
<ul>@foreach($menuitems 作为 $menuitem)<li>{{ $menuitem->title }}</li>@endforeach
最后我们可以通过 composer 注入变量.
with('menuitems', Menu::all());});
这样每次调用 parts/menu.blade.php
时,Composer 都会拦截视图并将其注入 $menuitems
变量.这与在 return View::make('blahblah')->with( 'menuitems', Menu::all() )
with
相同希望有帮助:)
<小时>编辑:如果你不喜欢在 routes.php
中有逻辑你可以把它放在 start.php
并考虑 JasonLewis 将 start.php
拆分成单独文件的方法.
在 application
中创建一个名为 start
的目录,并用一些文件填充它.
+ 应用程序 [DIR]->+ 开始 [目录]|->自动加载.php|->作曲家.php|->过滤器.php->验证.php
然后将这些代码行添加到application/start.php
需要 __DIR__ .DS.'开始' .DS.'自动加载.php';需要 __DIR__ .DS.'开始' .DS.'过滤器.php';需要 __DIR__ .DS.'开始' .DS.'作曲家.php';需要 __DIR__ .DS.'开始' .DS.'验证.php';
你明白了.将 Composer 函数放在 composers.php 中.
在此处阅读整篇文章:http://jasonlewis.me/article/laravel-使事情井井有条
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?
Note: this answer was written for Laravel 3 and might or might not work with the most recent 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)
<!-- 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.
<?php
// application/routes.php
View::composer('parts.menu', function($view){
$view->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() )
Hope it helps :)
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.
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
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';
You got the idea. Put the composer functions in composers.php.
Read the entire article here: http://jasonlewis.me/article/laravel-keeping-things-organized
相关文章