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

[eluser]HdotNET[/eluser]
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:

Code:
$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.

Controller:
Code:
class Cmspages extends Controller {

    function Cmspages()
    {
        parent::Controller();    
    }
    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):

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

Neither does this:


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

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

Code:
$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

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

[eluser]deviant[/eluser]
Are you sure it passes the wildcard as a function argument?
#3

[eluser]HdotNET[/eluser]
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.
#4

[eluser]deviant[/eluser]
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.
#5

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

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

thanks deviant...
#6

[eluser]frenzal[/eluser]
yep that'll do the trick
or you can just use $this->uri->segment(x)
#7

[eluser]HdotNET[/eluser]
true

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.
#8

[eluser]andho[/eluser]
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