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

[eluser]Animas[/eluser]
I guess url routing is not stable yet.
#12

[eluser]m4rw3r[/eluser]
Why not use:
Code:
$route['^([\d\w\-]+?)$'] = "test/blog/$1";
It matches all digits, word characters, '-' and '_' and must match the whole string (^ marks the start of the string and $ marks the end).

It does not match a "/", so if you enter "http://localhost/blogentry/comments", it will not match (but it will match "http://localhost/blogentry/" because CI removes all preceding and succeeding slashes).

If you need more characters, add them to the character class (a good reference on regular expressions are http://www.regular-expressions.info/).
#13

[eluser]Michael Ekoka[/eluser]
Hey Animas,

I see a couple of inconsistencies in your code.

1- First, the route
Code:
$route['([a-z0-9-]+)'] = "blog/post/$1";
You're effectively telling your route here to match everything after the base (www.site.com) and pass it as a back-reference to the route. This means that:
www.site.com/blog/post/post-title-a will be routed to blog/post/blog/post/post-title-a

You probably want to test your route as
www.site.com/post-title-a and www.site.com/post-title-b


2- The URI:Confusedegment() method
In your Blog::post() action you have this line:
Code:
$this->db->where('title', $this->uri->segment(3));

To fetch your controller's method's arguments you don't wanna use the URI:Confusedegment() method. This is for prerouted url. You rather wanna use the post routed segment method: URI::rsegment():
Code:
$this->db->where('title', $this->uri->rsegment(3));

Or better yet, these 2 alternative methods:
- if you know the number of arguments that you will receive, declare it in the action method:
Code:
class Blog{
    function post($post_title=null){
         ...
         $this->db->where('title', $post_title);
         ...
    }
}


- if you don't want to specify the list of arguments in the declaration, use either of the php functions func_get_arg() or func_get_args():
Code:
class Blog{
    function post(){
         $post_title = func_get_arg(0);
         ...
         $this->db->where('title', $post_title);
         ...
    }
}
#14

[eluser]Animas[/eluser]
Thanks m4rw3r. I used
Code:
$route['^([\d\w\-]+?)$'] = "blog/post/$1";
but still the issue remains. It didn't show any 404 error, so I think your regex works. So did previous regex I used. The only problem was(!) all post links goes to the first post only. And I learned some regex from you.

Now Ekoka. You pointed me to the magic word "rsegment". I didn't know it's use earlier. Now everything is working. Your help is much appreciated. And also thanks for taking the time to go through my code keenly.




Theme © iAndrew 2016 - Forum software by © MyBB