Welcome Guest, Not a member yet? Register   Sign In
route annoyance

Hi all,

The routes system is driving me up the wall. I'm sure its down to me, but would appreciate another set of eyes on this...

Routes file:

$route['pages/:num'] = "cmspages/viewpage";
$route[':any'] = "cmspages";

The cmspages controller is, um, a cms. The index method within the controller looks at all the uri segments in order to retrieve a page/provide a 404, perfectly handled by the :any route directive as shown above.

class Cmspages extends Controller {

    function Cmspages()
    function index()
//cms stuff
    function viewpage($num){
        echo 'param: '.$num;

The problem I am having is with the first pages/:num directive. The second uri segment (:num) should surely be passed as a param to the viewpage function in the controller and then echo'ed? It isn't.

This doesn't work either (no numeric param passed):

$route['pages/viewpage/:num'] = "cmspages/viewpage";
$route[':any'] = "cmspages";

Neither does this:

$route['cmspages/:num'] = "cmspages/viewpage";
$route[':any'] = "cmspages";

This DOES work as expected, but doesn't give the desired urls:

$route['cmspages/viewpage/:num'] = "cmspages/viewpage";
$route[':any'] = "cmspages";

Can anyone tell me what is wrong with this picture? Naturally I could use the URI object to get the desired param, but judging by the docs I shouldn't need to:

URI Routing

A typical wildcard route might look something like this:
$route['product/:num'] = "catalog/product_lookup";

Are you sure it passes the wildcard as a function argument?

Er no. I'm not entirely sure, but if it doesn't then it should!

At least I haven't used this framework enough to see why it shouldn't.

Well from what I read in the docs it doesnt say anything about passing it as an argument, so you might have to use regex instead to pass the value you want as a URI segment and get it that way.

this works like I expected $route['pages/:num'] should:

$route['pages/(\d+)'] = "cmspages/viewpage/$1";

thanks deviant...

yep that'll do the trick
or you can just use $this->uri->segment(x)


but the regex does the work of ensuring that the retrieved param is a number and that additional url params cannot be parsed to the function.

if that happens i think you have to use a non-greedy regex:
$route['pages/(\d+?)'] = "cmspages/viewpage/$1";

regex is a whole other world. and its hard to get there Tongue

Theme © iAndrew 2016 - Forum software by © MyBB