Welcome Guest, Not a member yet? Register   Sign In
URI Routing Like Wordpress Pages and Posts
#1

[eluser]cornofstarch[/eluser]
Hello,

I'm completely confused on how to do this correctly. I would like the routing to work similar to Wordpress routing.

For example, if you type in yoursite.com/about, it will go to the about page. But in this case, it would go to my "about" controller. I got as far as this:

routes.php
Code:
$route['^[0-9a-zA-Z][\w-]*[0-9a-zA-Z]$'] = "pages";

pages.php
Code:
class Pages extends Controller {

    function __construct() {
        parent::Controller();
    }

    function index() {
        $seg;
    
        if($this->uri->total_segments() == 1) {
            $seg = $this->uri->segment(1);
        }
        
        // check to see if the uri segment is a page in the database
        // if it matches and is in the database, redirect the visitor to that page
        
        redirect('login');
    }
}

But now when I want to redirect to my login controller, it breaks because it matches the condition in routes.php. I'm not sure how to proceed... can someone point me in the right direction on how to solve this?
#2

[eluser]RedIgniter[/eluser]
I am not really sure what your problem is, can you explain further?
#3

[eluser]Phil Sturgeon[/eluser]
This is a commonly asked question, answered on SO.

http://stackoverflow.com/questions/37250...54#3960354
#4

[eluser]Killswitch[/eluser]
You can put a route above that with controllers you want to preserve. For example:

$route['(login|register)'] = "$1";

That will make it so /login and /register go to your login and register controllers.
#5

[eluser]cornofstarch[/eluser]
Thank you everyone for the replies. I'm going to try out the methods to see which one gives me the greatest ease, control, and flexibility.
#6

[eluser]cornofstarch[/eluser]
Hi,

I'm trying this method but I'm having trouble with the remap...

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Pages extends MX_Controller
{

    function __construct()
    {
        parent::__construct();
    }

    function _remap($method, $params = array())
    {
        print $method;
        
        if(empty($params))
        {
            print " empty";
        }
        
        foreach($params as $param)
        {
            print $param . "<br />";
        }
    }
}

?&gt;

I keep getting "index" for $method and $params[] is always empty. For example, http://localhost/test/wow/again will result in the following output:

index empty

I'm trying to get the following output:

test wow
again

What am I doing wrong with the remap?
#7

[eluser]cornofstarch[/eluser]
For more details go here: http://ellislab.com/forums/viewthread/179020/

The CI manual really needs to be clearer on this for inexperienced people like me. -_-;

The example is localhost/test/wow/again. In config/routes.php, I routed all 404 errors to pages.php. Using the controller code above, $method would only show "index" because the method "test" doesn't actually exists in my Pages controller. If I were to add a new method...

Code:
public function test()
{
    print "hello world";
}

... then I would get the following result in my browser:

testwow
again

I hope this helps someone if they're exploring the remap stuff.




Theme © iAndrew 2016 - Forum software by © MyBB