Home > database >  Codeigniter v4:Custom View Does Not Return
Codeigniter v4:Custom View Does Not Return

Time:01-12

I'm using Codeigniter v4 and wanted to show a custom view to users, so I made a Controller named Test:

<?php

namespace App\Controllers;

class Test extends BaseController
{
    public function index()
    {
        $this->load>model('Usermodel');
        $data['users'] = $this->Usermodel->getusers();
        return view('custom');
    }
}

And a Model named Usermodel:

<?php

namespace App\Models;

class Usermodel extends CI_Model
{
    public function getusers()
    {
        return [
            ['firstmame'=>'Mohd','lastname'=>'Saif'],
            ['firstname'=>'Syed','lastname'=>'Mujahid'],
            ['firstname'=>'Mohd','lastname'=>'Armaan']
        ];
    }
}

And the view custom.php already exists in the Views folder.

But when I load the url http://localhost/ci4/public/index.php/test I get 404 Not Found error message.

Also I tried http://localhost/ci4/public/index.php/test/index but shows the same message.

So how to load this method from the custom controller class in Codeigniter v4 properly?

CodePudding user response:

Except you're not parsing the $data to the view (you should do that adding a second parameter to view('custom', $data)), the code doesn't seem to be the problem: When a view could not be loaded in CI, it shows a specific error message (CodeIgniter\View\Exceptions\ViewException), not a 404 Not Found message.

Probably the problem is in another part of your project.

CodePudding user response:

Try this

<?php

namespace App\Controllers;

class Test extends BaseController
{
    public function index()
    {
        $this->load>model('Usermodel');
        $data['users'] = $this->Usermodel->getusers();
        return $this->load->view('custom',$data);
    }
}

add the $data array to your views

  • Related