【发布时间】: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