Can't access custom view in CodeIgniter

时间:2018-09-19 08:26:38

标签: php codeigniter

I'm having problem when it comes to accessing the custom view that I made in CodeIgniter. Whenever I want to navigate to a list in which I am making as an app, it gives me a message saying, 404 Page Not Found even though I included the model I created in the autoload.php in the config folder. Here are some of my code in the list app that I am creating:

Here is my code in my controller called lists.php:

<?php 

    class Lists extends CI_Controller{
    // Display a specific list
    public function display_list($list_id){

        //display information about the list
        $data['list_data'] = $this->lists_model->get_list($list_id);

        $data['main_view'] = 'project_views/lists/display_list';

        $this->load->view('project_views/layouts/main', $data);
    }
?>

Here is my code in my model called lists_model.php:

<?php 

    class Lists_model extends CI_Model{

    public function get_list($list_id){
            $this->db->where('id', $list_id);
            $query = $this->db->get('lists');

            return $query->row();
        }
?>

Here is my code in my view called display_list.php

<div class="col-xs-9">

    <h3>List Name:<?php echo $list_data->list_name;?></h3>
    <h3>Created by:<?php echo $list_data->list_creator;?></h3>
    <h3>Lists Details:</h3>
    <p class="lists-description">
        <?php echo $list_data->list_details;?>
    </p>

</div>

Here is my code in my index.php in my views folder:

<h1>Lists made with Takenote</h1>
<p class="bg-success">

    <?php if($this->session->flashdata('list_created')): ?>
    <?php echo $this->session->flashdata('list_created'); ?>
    <?php endif; ?>

    <?php if($this->session->flashdata('list_updated')): ?>
    <?php echo $this->session->flashdata('list-updated'); ?>
    <?php endif; ?>

    <?php if($this->session->flashdata('list_deleted')): ?>
    <?php echo $this->session->flashdata('list_deleted'); ?>
    <?php endif; ?>
</p>

<div class="panel panel-primary">
    <div class="panel-heading"><h3>Lists</h3></div>
    <div class="panel-body">
        <ul class="list-group">

            <?php foreach($lists as $list): ?>

            <li class="list-group-item">
                <a href="<?php echo base_url();?>project_controllers/lists/create" class="btn btn-success pull-right">
                    Create a List
                </a>
                <h3 align="center">
                    <a href="<?php echo base_url();?>project_contollers/lists/display_list/<?php echo $list->id;?>">
                        <?php echo $list->list_name; ?>
                    </a>            
                </h3>
            </li>
        <?php endforeach; ?>
        </ul>
    </div>
</div>

I even included the model I created in the autoload.php in the config folder:

$autoload['model'] = array('project_models/user_model', 'project_models/project_model', 'project_models/tasks_model', 'project_models/lists_model');

However, even after doing this, I still get a message saying 404 Page Not Found when I try to navigate to a specific list:

2 个答案:

答案 0 :(得分:1)

请看看https://www.codeigniter.com/userguide3/general/routing.html#wildcards

它可以为您提供所需的详细信息。

我相信您需要为此应用程序添加自定义路由。

答案 1 :(得分:0)

粗心我。经过几分钟的调试,我已经解决了问题。我所缺少的是在index.php中添加链接的路径中的单个字母。这是我困在几分钟到一个小时的代码:

<a href="<?php echo base_url();?>project_contollers/lists/display_list/<?php echo $list->id;?>">
   <?php echo $list->list_name; ?>
</a> 

应该是这样的:

<a href="<?php echo base_url();?>project_controllers/lists/display_list/<?php echo $list->id;?>">
  <?php echo $list->list_name; ?>
</a>