Welcome Guest, Not a member yet? Register   Sign In
how to keep the variable passed to the view in codeigniter 4
#1

(This post was last modified: 02-26-2022, 01:46 AM by startup. Edit Reason: note for someone need )

in  my controller  Home.php
PHP Code:
class HomeController extends BaseController{
 
    
public function index(){
        $mHome = new HomeModel();
        $data['demo'] =  $mHome->ViewArticle('8','view');
        return view('\Modules\Site\Home\Views\home_index'$data);
    }

can you show me how to pass $mHome to view page?. i dont want to rewrite code again, i want in my view page below:

in my view.php

PHP Code:
$demo =$mHome->myFunction($id); 
thank you in advance. sorry i know a little english

thank you @ikesela, sometime is sptupid, i fix code above this, it working
PHP Code:
class HomeController extends BaseController{
 
    
public function index(){
        $mHome = new HomeModel();
        $data['demo'] =  $mHome->ViewArticle('8','view');
        
$data['mHome'] = $mHome;  //add line in code, perfect
        return view('\Modules\Site\Home\Views\home_index'$data);
    }

Reply
#2

(This post was last modified: 02-26-2022, 01:14 AM by ikesela.)

just pass the object:
Code:
return view('\Modules\Site\Home\Views\home_index', [ 'mHome' => new HomeModel() ]);
can access in view as $mHome
Reply
#3

You don't need to pass the model to the view.
This is bad practice.
Reply
#4

(This post was last modified: 02-26-2022, 01:56 AM by startup.)

(02-26-2022, 01:22 AM)iRedds Wrote: You don't need to pass the model to the view.
This is bad practice.

i need code below
PHP Code:
cateMenu =1
$getcateMenu 
=$mhome->function('1'

can you  good method to get id folow cate in view page ? i know 'view_ceil' in codeigniter 4 , but id dont like this function
Reply
#5

(02-26-2022, 01:22 AM)iRedds Wrote: You don't need to pass the model to the view.
This is bad practice.

Thank, i agree .
Reply
#6

(02-26-2022, 01:54 AM)startup Wrote:
(02-26-2022, 01:22 AM)iRedds Wrote: You don't need to pass the model to the view.
This is bad practice.

i need code below
PHP Code:
cateMenu =1
$getcateMenu 
=$mhome->function('1'

can you  good method to get id folow cate in view page ? i know 'view_ceil' in codeigniter 4 , but id dont like this function

I don't know the logic of your application.
For example, you can create a service that will prepare data for display.
This is a simple example to show the concept.

PHP Code:
class MyService
{
    public function 
doSomething()
    {
        
$data = ['...'];
        \
Config\Services::renderer()->setData($data);
        
        return 
$this;
    }

    public function 
doSomething2()
    {
        
$data = ['...'];
        \
Config\Services::renderer()->setData($data);
        
        return 
$this;
    }
}

class 
MyController extends Controller
{
    public function 
index()
    {
        
$ms = new MyService();
        
$ms->doSomething()->doSomething2();
        
        return 
view('template');
    }

Reply




Theme © iAndrew 2016 - Forum software by © MyBB