laravel 4-将多个控制器中的相似数据拉入许多视图 [英] laravel 4 - pull similar data in multiple controllers into many views
问题描述
我有这样的用户路线:
user/13/posts/10
用户/13/videos/443
用户/13/items/4002
user/13/posts/10
user/13/videos/443
user/13/items/4002
我想在每次加载帖子/视频/项目视图时提取与"user/13"相关的所有数据. 我还加载了侧边栏@include('inc/blog-sidebar'),该文件包含的数据在所有视图中都相同,并且与'user/13'相关.
And i want to pull all the data related to 'user/13' every time posts/videos/items views are loaded. i am also loading the sidebar @include('inc/blog-sidebar') and this file contain data that is the same in all the views and related to 'user/13'.
在所有控制器中不按功能进行提取所有信息的最佳方法是什么?
what is the best way to pull all this information without doing it per function within every controller?
我在routes.php中尝试过view:composer(这很糟糕),但我无法获取用户ID并获取相关信息.
I have tried view:composer within the routes.php (which is bad) and i could not get the user id and get the relevant information.
View::composer('posts.show', function($view)
{
$author = User::find(1);
$view->with('author',$author);
});
此外,如果这是最好的解决方案,那么我应该将View Composer存储在哪里?
also, if this is the best solution then where should i store my view composer?
谢谢!
推荐答案
要使用视图编写器自动用数据填充视图,请执行以下操作:
首先,为这样的URI
(如user/13
或user/15
)创建一个路由模式(在routes.php
文件中):
To automatically populate views with data using view composers:
At first create a route pattern for any URI
like user/13
or user/15
like this (In routes.php
file):
// Composers will be used only for this url pattern, i.e. "user/13" or "user/15"
Route::when('user/*', 'prepareView');
然后创建prepareView
过滤器,如下所示:
Then create the prepareView
filter like this:
Route::filter('prepareView', function($route, $request) {
// Register your three view composers
View::composers(array(
// Call the "postsViewComposer" method from
// ViewComposers for "posts.show" view
'ViewComposers@postsViewComposer' => 'posts.show',
// Call the "videsViewComposer" method from
// ViewComposers for "videos.show" view
'ViewComposers@videsViewComposer' => 'videos.show',
// Call the "itemsViewComposer" method from
// ViewComposers for "items.show" view
'ViewComposers@itemsViewComposer' => 'items.show',
));
});
然后在app/viewcomposers
文件夹中创建ViewComposers
类:
Then create the ViewComposers
class in app/viewcomposers
folder:
class ViewComposers {
public function postsViewComposer($view)
{
// Get user id, for example, 15
$id = Route::current()->parameter('id');
// Then use it to retrieve the model
$author = User::find($id);
return $view->with('author', $author);
}
public function videsViewComposer($view)
{
// Logic for this view composer
}
public function itemsViewComposer($view)
{
// Logic for this view composer
}
}
最后,您需要在autoload->classmap
的composer.json
文件中添加新的class
,例如:
Finally, you need to add your new class
in your composer.json
file in autoload->classmap
like:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
// ...
"app/viewcomposers"
]
最后,只需从项目的根目录中的command prompt/terminal
中运行composer dump-autoload
.而已.现在,每当请求具有任何id
的任何用户使用URI
时,这些view composers
都将运行以准备视图.
At last, just run the composer dump-autoload
from your command prompt/terminal
from within your project's root directory. That's it. Now whenever a URI
for any user with any id
will be requested, those view composers
will run to prepare the views.
这篇关于laravel 4-将多个控制器中的相似数据拉入许多视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!