[英]laravel 4 - pull similar data in multiple controllers into many views
I have users routes like that: 我有这样的用户路线:
user/13/posts/10 用户/ 13 /文章/ 10
user/13/videos/443 用户/ 13 /视频/ 443
user/13/items/4002 用户/ 13 /项目/ 4002
And i want to pull all the data related to 'user/13' every time posts/videos/items views are loaded. 我想每次加载帖子/视频/项目视图时提取与“ user / 13”相关的所有数据。 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'. 我还加载了侧边栏@include('inc / blog-sidebar'),此文件包含的数据在所有视图中都相同,并且与'user / 13'有关。
what is the best way to pull all this information without doing it per function within every controller? 提取所有这些信息而不在每个控制器中按功能执行的最佳方法是什么?
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. 我已经尝试了route.php中的view:composer(这很糟糕),但我无法获取用户ID并获取相关信息。
View::composer('posts.show', function($view)
{
$author = User::find(1);
$view->with('author',$author);
});
also, if this is the best solution then where should i store my view composer? 另外,如果这是最好的解决方案,那么我应该在哪里存储View Composer?
Thanks! 谢谢!
At first create a route pattern for any URI
like user/13
or user/15
like this (In routes.php
file): 首先,为这样的任何URI
例如user/13
或user/15
创建一个路由模式(在routes.php
文件中):
// Composers will be used only for this url pattern, i.e. "user/13" or "user/15"
Route::when('user/*', 'prepareView');
Then create the prepareView
filter like this: 然后创建如下的prepareView
过滤器:
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',
));
});
Then create the ViewComposers
class in app/viewcomposers
folder: 然后在app/viewcomposers
文件夹中创建ViewComposers
类:
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
}
}
Finally, you need to add your new class
in your composer.json
file in autoload->classmap
like: 最后,您需要在autoload->classmap
中的composer.json
文件中添加新class
,如下所示:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
// ...
"app/viewcomposers"
]
At last, just run the composer dump-autoload
from your command prompt/terminal
from within your project's root directory. 最后,只需从项目的根目录中的command prompt/terminal
运行composer dump-autoload
。 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. 现在,每当请求具有任何id
任何用户的URI
时,这些view composers
就会运行以准备视图。
I think you are talking about eager loading. 我认为您是在谈论急切的加载。 You can approach this way, 你可以这样
User::with('posts','videos')->get();
See, Eager Loading 请参阅急切加载
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.