skip to Main Content

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

<?php

namespace AppControllers;

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 AppModels;

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?

2

Answers


  1. Try this

    <?php
    
    namespace AppControllers;
    
    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

    Login or Signup to reply.
  2. 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 (CodeIgniterViewExceptionsViewException), not a 404 Not Found message.

    Probably the problem is in another part of your project.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search