Welcome Guest, Not a member yet? Register   Sign In
Regular expression & routing problem
#1

[eluser]Animas[/eluser]
I am developing a blog type app. www.site.com shows all blog posts. Function "blog/index" shows blog list, function "blog/post" shows individual post. I removed index.php with .htaccess.

I need to route www.site.com/post-title-a to www.site.com/blog/post/post-title-a. (assuming post-title-a is the title for first post, post-title-b for the 2nd post...)

I used the following code in routes.php.
Code:
$route['([a-z0-9-]+)'] = "blog/post/$1";

Now when I go to www.site.com/blog/post/post-title-a the first post is viewed fine. But when I go to www.site.com/blog/post/post-title-b it also shows the first post. In fact all post links shows the first post page only. What could be wrong here? Thanks.
#2

[eluser]xwero[/eluser]
What is the code you use to select the post content?
#3

[eluser]Animas[/eluser]
Please have a look at my code for any possible mistake I have made. Still I couldn't solve the issue.

blog.php
Code:
<?php
class Blog extends Controller
{
    function Blog()
    {
        parent::Controller();
        $this->load->scaffolding('entries');
        $this->load->helper('url');
        $this->load->database();

    }
    
    function index()
    {
        $data['title'] = "My Blog Title";
        $data['heading'] = "My Blog Heading";
        //$data['query'] = $this->db->get('entries');
        
        $this->load->library('pagination');
        
        $config['base_url'] = base_url().'blog/index/';
        $config['total_rows'] = $this->db->count_all('entries');
        $config['per_page'] = '2';
        
        $this->pagination->initialize($config);
                
        //load the model and get results
        $this->load->model('blog_model');
        $data['results'] = $this->blog_model->get_blogs($config['per_page'],$this->uri->segment(3));
        
        $this->load->view('blog_view', $data);
        
    }
    function post()
    {
        $data['title'] = "My Post Title";
        $data['heading'] = "My Post Heading";
        $this->db->where('title', $this->uri->segment(3));
        $data['query'] = $this->db->get('entries');
        $this->load->view('post_view', $data);
        
    }    
}
?>

blog_model.php
Code:
<?php
class Blog_model extends Model {
  function Blog_model(){
    parent::Model();
  }

  function get_blogs($num, $offset) {
    $query = $this->db->get('entries', $num, $offset);    
    return $query;
  }
}
?>

blog_view.php
Code:
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<h1>&lt;?php echo $heading;?&gt;</h1>
&lt;?php if ($results->num_rows() > 0): ?&gt;
    &lt;?php foreach($results->result() as $row):?&gt;
    &lt;?php $clean_title = str_replace('-', ' ', $row->title); ?&gt;
    <h3><a href="&lt;?=base_url()?&gt;&lt;?=$row->title?&gt;">&lt;?=$clean_title?&gt;</a></h3>
    <p>&lt;?=$row->body?&gt;</p>
    <hr />
    
    &lt;?php endforeach; ?&gt;
&lt;?php endif; ?&gt;

&lt;?php echo $this->pagination->create_links(); ?&gt;
&lt;/body&gt;
&lt;/html&gt;

part of config.php
Code:
$config['base_url']    = "http://localhost/ci/";

$config['index_page'] = "";

part of routes.php
Code:
$route['default_controller'] = "blog";
$route['([a-z0-9-]+)'] = "blog/post/$1";

.htaccess
Code:
#Options -indexes
#DirectoryIndex index.php
RewriteEngine On
RewriteBase /ci/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]

db table: entries
fields of entires: id, title, body
("title" field should take entries like "post-title-a")
#4

[eluser]bcorcoran[/eluser]
Make sure that your scaffolding trigger in your routes is before the custom route, I didn't see that part of your routes.php... that may be the issue.
#5

[eluser]Animas[/eluser]
[quote author="brendanc" date="1207637927"]Make sure that your scaffolding trigger in your routes is before the custom route, I didn't see that part of your routes.php... that may be the issue.[/quote]
brendanc it is in place. I didn't put it here because I thought it might not be relevant. But I learned a new thing from you. Thanks.

I still can't find out why all the post links fetches the first post only. (without routing all the post links fetches the respective posts)
#6

[eluser]bcorcoran[/eluser]
Hrm, ok. Well, you should consider that your regular expression is going to be a bit "greedy" when it comes to parsing the URL in this way.

I'll take a look at it myself and see if I can't come up with a Regex that works, but my fear is that you'll have to do some additional processing of the url input if you want to access anything but posts.

My thought is that with your current Regex, you're going to be capturing (inside the brackets):

www.example.com/[blog-post-title]
www.example.com/[blog/post/id]
www.example.com/[othercontroller/function]
www.example.com/[anothercontroller/function/id]

All with that one Regex... and they're all going to be passed to the /blog/post/$1 in your route.

This is the problem with a greedy Regex Tongue

So, like I said, you may need to route your URL to a separate controller and parse the URL from there and handle it accordingly... but I may be getting ahead of myself... I'll take a look at it Smile
#7

[eluser]bcorcoran[/eluser]
Also, try replacing this in your .htaccess:
Code:
RewriteRule ^(.*)$ index.php?$1 [L]

With:

Code:
RewriteRule ^(.*) index.php/$1
#8

[eluser]Animas[/eluser]
Thank you brendanc. I have changed .htaccess as you said but the issue remains unsolved.
sql of db (blog)
Code:
CREATE DATABASE `blog` DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci;
USE blog;

--
-- Table structure for table `entries`
--

CREATE TABLE `entries` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(128) collate latin1_general_ci NOT NULL default '',
  `body` text collate latin1_general_ci NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `title` (`title`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=11 ;

--
-- Dumping data for table `entries`
--

INSERT INTO `entries` VALUES (1, 'my-first-entry', '11111111111111');
INSERT INTO `entries` VALUES (2, 'my-second-entry', '222222222222222');
INSERT INTO `entries` VALUES (3, 'my-third-entry', '333333333333333');
INSERT INTO `entries` VALUES (4, 'my-fourtth-entry', '4444444444444');
INSERT INTO `entries` VALUES (5, 'my-5th-entry', '5555555555555555555555555');
INSERT INTO `entries` VALUES (6, 'my-6th-entry', '666666666666666666');
INSERT INTO `entries` VALUES (7, 'my-7th-post', '77777777777777');
INSERT INTO `entries` VALUES (8, 'my-8th-post', '88888888888888');
INSERT INTO `entries` VALUES (9, 'my-9th-entry', '99999999992');

My application will show list of blogs(links) on www.site.com and any links www.site.com/any will show post links. It will a real help for a novice for me to have a solution for this issue.

NB: a slight change in my routing rule to allow numbers in title
Code:
$route['([a-z0-9-]+)'] = "blog/post/$1";
#9

[eluser]Ryuuzaki92[/eluser]
[quote author="Animas" date="1207737388"]
Code:
$route['([a-z0-9-]+)'] = "blog/post/$1";
[/quote]

this will catch everything. i think you should hijack the 404 function to reroute to the controller and method. but i'm not sure...
#10

[eluser]Animas[/eluser]
And the issue remains unsolved...




Theme © iAndrew 2016 - Forum software by © MyBB