jeudi 3 mars 2016

Passing variables in common to controllers in Laravel 5.2

Both my AdminController and IndexController, two different controllers, one for public website and other for administration area, will have the same data from database to display. Listening to first Laracast's videos I know I can use the Controller class for such, which is a class that is used for writing a function that is common for all controllers, so I imagine I could do something like this.

Currently, my Controller is like this:

public function index()
{
    return \View::make('admin/index')->with(array(
        'posts' => \DB::table('posts')->get(),
        'users' => \DB::table('users')->get(),
        'comments' => \DB::table('comments')->get(),
        'likes' => \DB::table('likes')->get()
    ));
}

Which belonged to my AdminController:

class AdminController extends Controller 
{
    // ...
}

and my IndexController:

public function index()
{
    return \View::make('index');
}

that only returns the index view.

The Controller is rendering the index view from the admin folder, and since I render a different view from IndexController, I know these four variables are not accessible. I tried to create them within a __construct() inside of Controller, but it didn't work. How can I pass these four variables to both controllers and access them from their respective views?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1SmY1lF
via IFTTT

Aucun commentaire:

Enregistrer un commentaire