Welcome Guest, Not a member yet? Register   Sign In
Where place render page code?
#1

Hello everyone,

i need render page, but before i have a lot ruotine code, like set variables for template, get notifications for users choose template mobile or desktop, tell me please shopuld i put it code in model, helper?
I dont want copy past same code in each controller,

where better to put routine code for render page
Reply
#2

That should probably be in your controller. Then after loading what you need you determine what is the right view to load.
Reply
#3

Like a sad before, i dont want make copy past in each controller, i wanna make code dry
Reply
#4

First of all, you should read CI manual / documentation and from that understand what you can do / make hand-in-hand with what you need to achieve.
This forum is not meant for teaching you understand programming and developing, or do a work for you, but for solving issues and helping each other.

https://codeigniter4.github.io/CodeIgnit...youts.html
https://codeigniter4.github.io/CodeIgnit...dules.html


Long story short:
Extend controller to some kind of base controller, where base controller always collects all "routine" stuff. Use view layouts with extending and creating sections.


This is how i use CI benefits. This is jsut a basic and totally simple example.

App/Controllers/BaseController.php
PHP Code:
<?php 
namespace App\Controllers;

use 
CodeIgniter\Controller;

class 
BaseController extends Controller
{
 public 
object $data;

 public function 
initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        parent::initController($request$response$logger);

        $this->initData();
    }

    protected function initData()
    {
    // collect all "routine" code, data, etc...
    $this->data = (object) [
        'layout' => (object) [
        'header_title' => '',
        ]
        'menu' => [
        0 => [
        'title' => 'Contact Us',
        'url' => '/contact-us',
        ],
        1 => [
        'title' => 'About us',
        'url' => '/abour-us',
        ],
        ],
        'misc' => [],
        ];
    }

    public function render(string $name): string
    
{
        return view($name, (array) $this->data);
    }




App/Controllers/MainController.php
PHP Code:
<?php 
namespace App\Controllers;

use 
App\Controllers\BaseController;

class 
MainController extends BaseController
{
 public function 
initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        parent::initController($request$response$logger);
    }

    public function index(): string
    
{
    $this->data->layout->header_title 'Homepage';

        return $this->render(__FUNCTION__);
    }




App/Views/layout.php
PHP Code:
<!DOCTYPE html>
<
html lang="en" dir="ltr">
 <?=
$this->include('/views/layout_header');?>
 <body>
 <?=$this->renderSection('content');?>
 </body>
 <?=$this->include('/views/layout_footer');?>
</html> 


App/Views/layout_header.php
PHP Code:
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title><?=$layout->header_title;?></title>
</head> 


App/Views/layout_footer.php
PHP Code:
<footer>
 <?
php foreach ($menu as $link) { ?>

 <a href="<?=$link['url'];?>">
 <?=$link['title'];?>
 </a>

 <?php ?>
</footer> 



App/Views/index.php
PHP Code:
<?=$this->extend('App\Views\layout');?>

<?=$this->section('content');?>

 <h1>Hello, World!</h1>
<p>This is <?=$layout->header_title;?></p>

<?=$this->endSection();?>
Reply
#5

What is the most convenient method

for example, in the controller
Code:
<code>function action_index(){

$my_var = "test";

$this-&gt;template-&gt;my_var = $my_var;

$this-&gt;template-&gt;render();

}
</code>

or in the model
<code>class Model_my_model extends Model{

function my_model(){

$my_var = "test";

$this-&gt;template-&gt;my_var = $my_var;

$this-&gt;template-&gt;render();

}

}
</code>


A:

You could create a base controller with the common code and extend it in the other controllers.
Code:
<code>class Controller_Base extends Controller
{
    public function before()
    {
        parent::before();

        // do common stuff here
        $my_var = "test";
        $this-&gt;template-&gt;my_var = $my_var;
    }
}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB