Welcome Guest, Not a member yet? Register   Sign In
URL route and empty var
#1

[eluser]Spir[/eluser]
Hi.

I have made a method within a controller. I use that method to output some JSON.
If I want one element i set the ID in parameter.
If I want the whole list I don't give the ID but I can set a limit and the number of the first element in the list (for pagination).

so I have this URLs :

website.com/controller/json/<id> -> in order to get one element with id in param
website.com/controller/json/-> in order to get the whole list
website.com/controller/json/<0>/<50> -> in order to get the first 50 elements in the list.
website.com/controller/json/<40>/<10> -> in order to get the 40th to the 49th elements in the list.

My method is :
Code:
function Json($id='', $start='', $step=''){
...
}
I set y route :
$route['controller/json/(:num)/(:num)'] = "controller/json/??/$1/$2";
$route['controller/json/(:num)'] = "controller/json/$1";
$route['controller/json/'] = "controller/json/"; // optional?

What should I put where I have my ??

Maybe I can't set a param to null directly in route so maybe I should set my function like this :

Code:
function Json($param_one, $param_two){
// And here check if there is only 1 param set then get an element for that ID
// or get the list using the 2 param
...
}
But it sucks...
#2

[eluser]louis w[/eluser]
You do not need any of that route stuff.

CI will automatically call the json method of your controller with the first and second param. Routes are only if you want to break the built in CI logic.

I would do something like this in my method (after removing all routes):

Code:
public function json($one=null, $two=null) {

    if (is_null($two)) {
        // Call model with value of $one to get just one item, by id

    } else {
        // Getting a list of items
        
        $start     = !is_null($one) ? $one : 0;
        $end     = !is_null($two) ? $two : 1000; // set some kind of default limit here
        
        // Call model
    
    }

}
#3

[eluser]Spir[/eluser]
Ok. I don't really like the idea of having params that can have various function depending of the call. But if there is no way to do it like I wanted (3 params).
#4

[eluser]louis w[/eluser]
[quote author="Spir" date="1228515315"]Ok. I don't really like the idea of having params that can have various function depending of the call.[/quote]

Unless I am misunderstanding your original request, that is what you are asking for. Either there is one param, which is the id - or there are two where the first is the starting number and the second is the count of items to get.

If you are very against doing the logic in the method and want two distinct methods for each action, add a _remap to your controller and switch there.

Just trying to steer you away from routes, it just seems like overkill to me. When at all possible I try to keep my application logic in the controller. Routes are just another thing to remember are in place and could cause future confusion.
#5

[eluser]Spir[/eluser]
I agree with you about trying to avoid route settings in apps.
But I just prefer to have each parameters for a specific needs : one for ID, one for start and the last one for amount to get (or end). So I prefer to setup a rules in my route. But it seems it's not possible to set an empty value to a method directly in route conf.

So I'll do what you have suggested : one method with 2 param. if one param is set then return the element identified by the id otherwise get the list with the specific values.

Thanks for your responses.
#6

[eluser]majidmx[/eluser]
First of all it's better to move the ID section to the end of your function parameters :
Code:
function Json($start='', $step='' , $id=''){
...
}

And be consistent all the time in your function calls :

Code:
website.com/controller/json/0/1/id
website.com/controller/json/
website.com/controller/json/<0>/<50>
website.com/controller/json/<40>/<10>

Hope it helps,
MajiD Fatemian




Theme © iAndrew 2016 - Forum software by © MyBB