Welcome Guest, Not a member yet? Register   Sign In
Page Slugs
#1

[eluser]derekmichaeljohnson[/eluser]
Let's say I have a bunch of "pages" in my database, each of which containing a unique "slug." So, for example, a page titled "Things To Do Downtown" would have a slug of "things-to-do-downtown." But instead of accessing each page by using a URI structure like: http://example.com/pages/view/things-to-do-downtown/, I'd like to use this: http://example.com/things-to-do-downtown/.

My initial thought was to extend the CI controller class, and add this to the constructor:
Code:
// check if first URI segment matches any page slugs
$q = $this->db
        ->where('slug',$this->uri->segment(1))
        ->get('pages');

if( $q->num_rows() > 0 )
{
    // page found, pass page content to view
    $data['page'] = $q->row();
    $this->load->view('page_view',$data);
}
But this doesn't seem to be an ideal solution. Can anyone suggest an alternative?

PS A friend suggested I take the config/routes approach, but wasn't quite sure how I would be able to distinguish between a page slug and controller name. So if I called http://example.com/something, how would it know if I wanted the page title "Something" or a controller by the same name?
#2

[eluser]Eric Barnes[/eluser]
Depending on your other needs it could be as simple as using the _remap method in your default controller. Here is an example that I recently used for this:
https://gist.github.com/813062
#3

[eluser]derekmichaeljohnson[/eluser]
Eric,

Thanks, man. I didn't even know this existed, and sure enough it's exactly what I need.

Cheers!
#4

[eluser]Vheissu[/eluser]
You are using routes as well, right? In your application/config/routes.php put in the following:

$route['default_controller'] = "pages";
$route['404_override'] = "pages";

This assumes that you are using the latest version of CI 2.0 of course. Then all URL requests are redirected to your pages controller. Then as Eric said, use a _remap() function to get the segments and do some database look ups.
#5

[eluser]derekmichaeljohnson[/eluser]
So how do I then call controllers/methods that aren't page related? I only want to display a "page" if there's a page that matches the request. Take for example:
Quote:http://example.com/testing/
Ideally, if there's a page with the slug "testing" it will display that page (using the pages controller), but if there ISN'T a slug "testing" found, it will use CodeIgniter's typical approach and look for a controller called "testing."

Is this possible?
#6

[eluser]Vheissu[/eluser]
[quote author="derekmichaeljohnson" date="1297035029"]So how do I then call controllers/methods that aren't page related? I only want to display a "page" if there's a page that matches the request. Take for example:
Quote:http://example.com/testing/
Ideally, if there's a page with the slug "testing" it will display that page (using the pages controller), but if there ISN'T a slug "testing" found, it will use CodeIgniter's typical approach and look for a controller called "testing."

Is this possible?[/quote]

The code I posted will do this. If you use 404_override it will send all requests it can't find matching controllers for to the pages controllers. Then in your pages controller you do the checking to see if the page exists and if not, then use the show_404() or show_error() functions. Simple.
#7

[eluser]derekmichaeljohnson[/eluser]
Thanks, man. I didn't realize 404_override was the key. In fact, _remap() isn't even really necessary after implementing 404_override.

Cheers!
#8

[eluser]Vheissu[/eluser]
[quote author="derekmichaeljohnson" date="1297049860"]Thanks, man. I didn't realize 404_override was the key. In fact, _remap() isn't even really necessary after implementing 404_override.

Cheers![/quote]

No problem Derek, any time!




Theme © iAndrew 2016 - Forum software by © MyBB