Welcome Guest, Not a member yet? Register   Sign In
Moving from CI3 - 4
#1

Hey

I'm having a bit of an issue when attempting to rewrite a sloppy program I made in CI3 to CI4.
I'm not the greatest coder, heck I struggled with Models hence why I never used them in CI3. (Sorry!) But want to make an effort now.

Say I had this in CI3 in my Controller

PHP Code:
    public function brewbeer()
    {
        
        
$get_beer $this->db->get_where('beer', array(
            
'id' => $this->uri->segment(3)
        ));
        
$data['query'] = $get_beer;
            
        if (
$get_beer->num_rows() <= 0) {
            
redirect('/recipes/');
        }
        
        
$data['main_content'] = 'brew-beer';
        
$this->load->view('../modules/template/template'$data);
    } 

Then in my view i had this etc eg
PHP Code:
<?php foreach ($query->result() as $beer): ?>

<?=$beer->name;?>

<?php endforeach; ?>


How best should this be done for CI4 as well as a model if needed? As some functions have changed and I'm struggling to get to grips with it. Should I handle error or redirect as I did before etc.

How would you handle something like this?
Reply
#2

(This post was last modified: 02-02-2020, 01:34 AM by littlej.)

Hello !

Here are some advices I would give you:
1/ I won't use things like "$this->uri->segment(3)", instead i would use routes definition: https://codeigniter4.github.io/userguide...aceholders
2/ I would use a model, yes. In CI3, I found it complicated, but in CI4 it is super easy: https://codeigniter4.github.io/userguide...your-model

So, I would use routes placeholders to manage incoming requests, create a "BeerModel" to interact with the database (and fetch the content), use the controller to manage all this and send the result to the view.

Please note that CI4 use a different way to load views: https://codeigniter4.github.io/userguide...ing-a-view
Reply




Theme © iAndrew 2016 - Forum software by © MyBB