Php 带有侧边栏内容的Laravel路由页面
我创建了一个Laravel博客,路由的工作方式是每个页面都有自己的Php 带有侧边栏内容的Laravel路由页面,php,laravel,Php,Laravel,我创建了一个Laravel博客,路由的工作方式是每个页面都有自己的Route::get('params') 这很好,我可以根据需要将特定内容发送到每个页面 问题是我试图发送侧边栏内容,但我试图防止像这样反复编写相同的代码: Route::get('/', function() { $sidebarContent = Photo::orderBy('id', 'DESC')->take(9)->get(); $posts = Post::orderBy('id', 'D
Route::get('params')
这很好,我可以根据需要将特定内容发送到每个页面
问题是我试图发送侧边栏内容,但我试图防止像这样反复编写相同的代码:
Route::get('/', function()
{
$sidebarContent = Photo::orderBy('id', 'DESC')->take(9)->get();
$posts = Post::orderBy('id', 'DESC')->get();
return View::make('index')->with('sidebarContent', $sidebarContent)
->with('posts', $posts);
});
Route::get('about', function()
{
$sidebarContent = Photo::orderBy('id', 'DESC')->take(9)->get();
return View::make('about')->with('sidebarContent', $sidebarContent);
});
最好的方法是什么?以下是我应该采取的方法吗
Route::group(array(), function()
{
$sidebarContent = Photo::orderBy('id', 'DESC')->take(9)->get();
Route::get('/', function($sidebarContent)
{
$posts = Post::orderBy('id', 'DESC')->get();
return View::make('index')->with('posts', $posts)
->with('sidebarContent', $sidebarContent);
});
Route::get('about', function($sidebarContent)
{
return View::make('about')->with('sidebarContent', $sidebarContent);
});
});
我会这样做。:)
您需要仔细阅读视图生成器-您认为这是一个好的解决方案吗?
Route::get('/', array('uses' => 'Controller@index'));
Route::get('user', array('uses' => 'Controller@about'));
class Controller extends BaseController{
protected $sidebarContent;
public function __construct(){
$this->sideBarContent()
}
public function index(){
$posts = Post::orderBy('id', 'DESC')->get();
return View::make('index')->with('sidebarContent', $this->sidebarContent)
->with('posts', $posts);
}
public function about(){
return View::make('about')->with('sidebarContent', $this->sidebarContent);
}
public function sidebarContent(){
$this->sidebarContent = Photo::orderBy('id', 'DESC')->take(9)->get();
}
}