【问题标题】:codeigniter routing wont route correctlycodeigniter 路由未正确路由
【发布时间】:2014-12-18 10:04:28
【问题描述】:

我是 codeigniter 的新手,我现在正在做一个项目.. 我有一个控制所有页面的家庭控制器,它工作正常。 该网站包含一个文章页面,其中显示所有文章,如果有人点击阅读更多,它将重定向到文章详细信息页面

问题是如果我点击文章页面上的阅读更多链接(例如:http://example.com/articles_details/2/sample-slug),它会自动重定向到http://example.com/articles_details/2 ....有人可以帮我解决这个问题。

下面是我的项目文件的详细信息

文件夹结构

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

家庭控制器

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 文件

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

articles.php(查看文件)

<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(查看文件)

var_dump($articledetails);

【问题讨论】:

  • 你做错了。如果您只想要本地访问,私有功能很好,那么为什么本地访问功能应该重定向您无法访问它的页面?
  • 正如我所说,我是 codeigniter 的新手,并且通过参考教程来做到这一点.. 那么访问它的正确方法应该是什么
  • 如果您是 CI 新手,最好的办法就是从头到尾阅读官方 CI 文档。它不仅仅是参考资料,实际上非常容易阅读/理解。它充满了演示和教程示例,您可以随时尝试。慢慢来,您仍然可以在 4 小时内完成整个工作。

标签: php codeigniter


【解决方案1】:

您应该有一个名为 article_details 的控制器,或者在您的主​​控制器中,将函数定义为 article_detailsindex(存在)。但不要通过函数来​​控制一切,虽然你可以拥有很多。

所以正确的方法是你应该有一个index 函数,并获取行然后将其传递给视图以显示它。

在你看来:

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

在您的控制器中,定义一个名为:article_details 的函数。它将是公开的,您可以处理文章 ID 并获取详细信息并将其传递给视图。

【讨论】:

    【解决方案2】:

    作为 CI 2.2.0 ,以下划线 _ 开头的方法或函数

    “..不会通过 URL 请求提供服务..”

    have a look here, and find PRIVATE FUNCTIONS

    这里的逻辑问题是为什么要将VISIBLE 控制器/方法 重定向到HIDDEN 一个?。您将无法访问任何内容,因为您首先想要隐藏。

    删除下划线并将属性更改为公共。如果您希望 int 被正确访问。

    或者您可能有错字或忘记输入您正在学习的教程中的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-29
      • 1970-01-01
      • 2016-06-18
      • 2012-10-04
      • 1970-01-01
      • 2019-10-11
      • 2021-04-27
      • 2023-03-14
      相关资源
      最近更新 更多