duandingcu7010 2014-12-18 10:04
浏览 61

codeigniter路由不能正确路由

I'm new to codeigniter and i'm doing a project now.. I have a home controller which controlls all the pages and it is working fine. The website contains an article page where it displays all the articles and if some one clicks on the read more it will redirect to article details page

The problem is if i click the read more link (eg : http://example.com/articles_details/2/sample-slug) on the articles page, it automatically redirects to http://example.com/articles_details/2 .... can somebody help me to fix this.

below is the details of my project files

Folder structure

website_folder/ 
–––– application/
---------- controllers/
---------------- admin/
---------------- home.php
---------- views/
---------------- templates/
--------------------- articles.php
--------------------- article_details.php
---------------- _main_layout.php
–––– assets/ 
–––– system/ 
---- .htaccess 
–––– index.php

home controller

class Home extends Frontend_Controller 
{
    function __construct()
    {
        parent::__construct();
    }   

    public function index()
    {
        $slug = $this->uri->segment(1) ? $this->uri->segment(1) : 'home';
        $this->data['page'] = $slug;

        $method = '_'.$slug;

        if(method_exists($this,$method))
        {
            $this->$method();
        }
        else
        {
            show_error('Could not load template '.$method); 
        }

        $this->data['subview'] = $slug;

        $this->load->view('components/page_head');
        $this->load->view($layout,$this->data);
        $this->load->view('components/page_tail');
        $this->load->view('components/'.$script); 
    }

    private function _home()
    {
        //fetch datas to be shown in homepage
    }

    private function _articles()
    {
        //fetch datas to be shown in article page
    }

    private function _article_details()
    {
        //fetch datas to be shown in article detail page
        $aid = $this->uri->segment(2);
        $query =  $this->db->query("select * from articles where id = ".$aid);
        $this->data['articledetails'] = $query->row();
        count($this->data['v']) || show_404(uri_string());
        $rslug = $this->uri->segment(3);
        $sslug = $articledetails->slug;
        if($rslug != $sslug)
        {
            redirect('article_details/'.$aid.'/'.$sslug,'location','301');
        }
    }

}

routes.php

$default_controller = "home";
$controller_exceptions = array('admin');

$route['default_controller'] = $default_controller;
$route["^((?!\b".implode('\b|\b', $controller_exceptions)."\b).*)$"] = $default_controller.'/index/$1';
$route['404_override'] = '';

Htaccess File

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>

articles.php (views file)

<div id="articles">
<?php if($pagination) echo $pagination; ?>
<ul class="articles">
  <?php 
  if(count($articles)) 
  {
      foreach($articles as $val)
      {
      ?>
      <li>
      <h2><?php echo $val->heading; ?></h2> 
      <p><?php echo truncate(strip_tags($val->details),200); ?></p>
      <a href="article_details/<?php echo $val->id."/".$val->slug; ?>" target="_blank">[ Read More ]</a>
      </li>  
      <?php 
      }
  }
  else
  {
      echo '<li><strong>No records found..</strong></li>';
  }
  ?>
</ul>
</div>

articles_details.php (views file)

var_dump($articledetails);
  • 写回答

2条回答 默认 最新

  • dongshi6529 2014-12-18 11:06
    关注

    You should have a controller named article_details or in your main controller , define a function as article_details and index(as exist) . but Do Not control everything by a function , while you can have lots of it.

    So the right way is that you should have a index function , and fetch rows then pass it to the view to show it.

    in your view :

    foreach($articles as $i){
            echo '<a href="'.site_url('{your_controller}/article_details/').$i->id.'/'.$val->slug.'" target="_blank">[ Read More ]</a>';
        }
    

    And in your controller , define a function named : article_details . It will be public and you can handle the article ID and fetch details and pass it to the view.

    评论

报告相同问题?