Welcome Guest, Not a member yet? Register   Sign In
Best way create layouts library vs extend CI_Controller

Hello guys

I'm start working with CI4 for testing propose and prepare the future migrations on my company website.

I have one simply question, Layout?

In the past i used the avenir approach (https://avenir.ro/create-cms-using-codei...dmin-area/), I Public_Controller with 1 layout and have Backoffice_Controller with another layout. It works fine for me.

But I also see another approach, the Library approach, (some old stuff here https://github.com/philsturgeon/codeigniter-template)

I recently find another one the Hook approach, (https://stackoverflow.com/questions/2956...nd-layouts)

So what is your opinion, what is the best approach for creating a layout system for CI4?

I like the extend My_controller approach, but I really confuse with the best way to do thing in CI4

Some time ago, one approach I used was to create a library "App\Libraries\Theme".

Then, use it as a service. Or, setting in a property of an abstract BaseController.

It could be used like "$this->theme->render('view')", for example.

To expedite, it has a "theme config". That could be set in the BaseController or other site area controllers that extends it. Then, many themes are possible. The "render" method automatically renders the view in the theme path, like "APPPATH . 'Views/theme-x/view-name.php'".

The data of head, header, footer, etc, are passed with a setData() method. That allow pre-set array keys in the BaseController, like an User object created according the Session user_id.

Hope it help.

In the past I've created a ThemedController that any controller could extends that provided a simple template system.

And, in CI4, I've even managed to extend the View library and provide some traditional template-like methods, including extending layouts, sections for dropping content into, etc. The secret sauce there is that you must remember when the views are rendered, they are in the scope of the View library that's rendering them. So you can add additional methods to your overridden class, and add whatever functionality you want.

Theme © iAndrew 2016 - Forum software by © MyBB