dimanche 3 janvier 2016

How to load data in pre-controller and pass drectly to view in Laravel 5

I am developing a laravel 5 project. That is an e-commerce project. What my problem is I want to load all categories for every request. Because that are used in navigation bar. Before I was usign Codeigniter Framework. In Codeigniter, I did like this. I created a Controller class named "MainController" that extends CI_Controller. So the class details is like.

class MainController extends CI_Controller{

public $data;

function __construct()
{
    $this->data["categories"] = getCategries();
}

}

Then every controller extends MainController and pass data to view like this.

class Home extends MainContoller{

 function index(){
  $this->load->view('view',$this->data);
 }

}

I tried the similiar way in Laravel. But data property values are not included when it reached to the child controller.So I tried to use Middleware. Then tried to pass the data together with request to the controller. But controller is still having to do something like this

   $data['categories'] = $request->categories;

I will need to do it in every action. I do not like it. So please how can I achieve it like in Codeigniter. So please what is the best way to do it in Laravel. I am currently using Laravel 5.1 .



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire