简体   繁体   中英

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

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!

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.

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

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

See, Eager Loading

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM