Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 带有侧边栏内容的Laravel路由页面_Php_Laravel - Fatal编程技术网

Php 带有侧边栏内容的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

我创建了一个Laravel博客,路由的工作方式是每个页面都有自己的
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();

    }
}