laravel 4 - pull similar data in multiple controllers into many views

余生颓废 提交于 2020-01-17 05:19:15

问题


I have users routes like that:

user/13/posts/10
user/13/videos/443
user/13/items/4002

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?

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);
});

also, if this is the best solution then where should i store my view composer?

Thanks!


回答1:


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');

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',
    ));

});

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
    }
}

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"
]

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.




回答2:


I think you are talking about eager loading. You can approach this way,

User::with('posts','videos')->get();

See, Eager Loading



来源:https://stackoverflow.com/questions/23968279/laravel-4-pull-similar-data-in-multiple-controllers-into-many-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!