Laravel 4,查看带参数的composer?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16178549/
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:44:08  来源:igfitidea点击:

Laravel 4, view composer with parameters?

laravellaravel-4

提问by user2094178

Suppose I have this:

假设我有这个:

//controllers/BlogController.php
$data["post"] = $post = Blog::recent_post();
$data["posts_related"] = Blog::posts_related($post->category_id,5);
return View::make('blog.home', $data);

//views/sidebars/related.blade.php
@foreach($posts_related as $r)
<p>{{ $r->name }}</p>
@endforeach

//views/blog/home.blade.php
@include('sidebars.related')

My question is, how could I transfer:

我的问题是,我怎么能转移:

$data["posts_related"] = Blog::posts_related($post->category_id,5);

To a view composer, since it seems I can't pass parameters to a view composer, but I can't be sure.

对于视图作曲家,因为我似乎无法将参数传递给视图作曲家,但我不能确定。

I appreciate any help!

我感谢任何帮助!

回答by Yahia Reyhani

In View Composer you can retrive data that passed to View.

在 View Composer 中,您可以检索传递给 View 的数据。

View::composer('sidebars.related', function($view)
{
   //$data contains category_id
   $viewdata= $view->getData();

   //Retrive based on category_id
   $posts_related = Blog::posts_related($viewdata['category_id'],5);

   //pass related post to view
   $view->with('posts_related', $posts_related);
});

now everywhere you can use:

现在到处都可以使用:

 View::make('sidebars.related')->with(array('category_id' => 6));