CodeIgniter框架加载文件失败求助:数据库数据展示代码异常
Let's break down the problems in your code and fix them step by step:
1. Invalid View Path (Root Cause of the Error)
The line $this->load->view('pages/pelayan/',$page); is triggering the "Unable to load the requested file" error because:
- You’re specifying a directory (
pages/pelayan/) instead of a specific view file. CodeIgniter requires the exact filename (without the.phpextension) to load a view. - The trailing slash at the end is invalid—it makes the framework look for a file named
/, which doesn’t exist.
Fix:
If your view file is named index.php inside application/views/pages/pelayan/, update the line to:
$this->load->view('pages/pelayan/index', $halaman);
(Note: I changed $page to $halaman since you defined $halaman['page'] = $page; earlier—this ensures the view can access the $page variable correctly.)
2. Model Loading Placement (Best Practice)
While loading the model after the first view won’t break functionality, it’s better to load models at the start of your controller method (or in the constructor) so they’re available when you need them. This makes your code cleaner and avoids potential issues later.
Updated Controller Code:
// Assign page data $halaman['page'] = $page; // Load model first (cleaner practice) $this->load->model('model_pelayan'); // Load the first view with correct path and data $this->load->view('pages/pelayan/index', $halaman); // Fetch data from model $data['devices'] = $this->model_pelayan->cekmeja()->result(); // Load the second view $this->load->view('cekmeja', $data);
3. Verify Model File Naming
Ensure your model file is named model_pelayan.php (exact match to the class name, case-sensitive on some servers) and placed in application/models/. CodeIgniter relies on matching filenames to class names for proper autoloading.
4. Optional: Simplify Model Method
Your model’s cekmeja() method returns the query object, which works, but you can simplify it to return the result directly:
// In model_pelayan.php function cekmeja() { return $this->db->get('devices')->result(); } // Then in controller: $data['devices'] = $this->model_pelayan->cekmeja();
This cleans up the controller code without changing functionality.
After making these changes, the "Unable to load the requested file" error should disappear, and your database data should load correctly into the view.
内容的提问来源于stack exchange,提问作者Me Myself

