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);
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.
As CI 2.2.0 , method or functions that starts with an underscore _
"..will not be served via a URL request.."
. have a look here, and find PRIVATE FUNCTIONS .
The logical question here is why would you want to redirect a VISIBLE
controller/method to a HIDDEN
one?. you will not be able to access anything since you want in Hidden the first place.
Remove the underscores and change the property to public. If you want int to be accessed correctly.
Or maybe you have a typo or forgot to type something from the tutorial you were following.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.