Welcome Guest, Not a member yet? Register   Sign In
Not compatible ? : "_remap" function and "Passing URI Segments to your Functions"
#1

[eluser]Solito[/eluser]
Hello,


I would like to use remap function but it annihilates the possibility to passing URI segments to my function.

Here is a simple example :

Code:
function _remap($methode)
{
    $this->$methode) ;
}
    
function test($var1='',$var2='')
{
    echo "var1 = $var1<br/>var2 = $var2" ;
}

If you comment the function _remap to disable it, it works :
- http://example.com/controller/test/shoes/02 <- this url allows you to pass segments 3 et 4 to your function

When you uncomment the function _remap, segments 2 and 3 cannot be no more atribued automatically to my function.


Have any idea why _remap acts like this ?


Solito
#2

[eluser]sophistry[/eluser]
hola Solito,

¡bienvenidos a codeigniter!

the line
Code:
$this->$methode;
does not pass any parameters so why would you expect parameters to be received at the test() method?

if you use _remap like that, you have to use the URI object to access the URI segments. in an earlier version of CI you *always* had to access segments via the URI object.

cheers.
#3

[eluser]Solito[/eluser]
Hello Sophistry,

It seems logical to you that I must use the URI object in that case but look :

- In my first example, we don't have to use *explicitly* the URI object to access segments 2 and 3 : it works without using $this->uri->segment, okay ?

- So tell me how could we guess that this *silent* use of URI object is no longer possible when using remap ?

What's more, Remap accepts one parameter only.
It means that I would have to pass the rest of the segment as an array to the remaped function. There isn't another way ?
#4

[eluser]sophistry[/eluser]
hi solito,

thank you for the spirited and learned discussion!

[quote author="Solito" date="1253127522"]- In my first example, we don't have to use *explicitly* the URI object to access segments 2 and 3 : it works without using $this->uri->segment, okay ?[/quote]
yes, that is true. But that is because the CI core is passing the parameters to the controller function. i'm not saying "that's the way it should be" just "that's the way it is". :-)
[quote author="Solito" date="1253127522"]
- So tell me how could we guess that this *silent* use of URI object is no longer possible when using remap ?
[/quote]
as i said in my first reply, the reason the parameters are not passed to the test() method (in your example) is because the code you wrote is not passing them from within _remap() when it calls
Code:
$this->$methode;
[quote author="Solito" date="1253127522"]
What's more, Remap accepts one parameter only. It means that I would have to pass the rest of the segment as an array to the remaped function. There isn't another way ?[/quote]

yes, that is true. i suggest rewriting the test() method so that it uses the native URI object. that way, you can be sure that the controller function will work whether it is called by the CI core or by your _remap() function.

alternatively, you could look at re-writing _remap() and figure out how to pass parameters automatically like the CI routing class does.

of those two ideas, i would choose the easier one. but, that's just me.

cheers.

EDIT: by the way, please look at how I use _remap() in the site_migrate wiki page linked in my signature below each post. i faced the same issue and after writing some code that would pass the ruri string out of the _remap() method, i decided to just use the URI object in the receiving controller methods.

i don't recommend this, but you might like it:
Code:
$this->$methode($this->uri->ruri_string());
#5

[eluser]Solito[/eluser]
Shhhh.... I'm so disappointed : I hoped so much that this writing would simplify my code.

But yes, you're right, it's the only way to achieve that simply : using URI object in the final method, not relaying on remap who can't handle all the situations to come.

I started looking at your page in the Wiki, it's now clearer for me how to use remap.

I will instead expand the constructor.


Thank you for the wery well documented answer.

And I'm sure we both learned something, didn't we ?


Thanks again,

Solito
#6

[eluser]sophistry[/eluser]
thank you for the compliment.

here is a thread post where the dev has hacked the CI core file to make _remap() behave the way you wanted:

http://ellislab.com/forums/viewthread/11...15/#574424
#7

[eluser]Solito[/eluser]
Ok, thanks.

So I'm not the only one who noticed that _remap upgrading could be a good idea, in some sort of way.

Thanks again for your support, it's been very clear.
#8

[eluser]sophistry[/eluser]
i've been thinking about this and i think i now agree that _remap should natively be passed all segment parameters. right now, _remap() only gets the controller method name.

originally, CI controller functions could not receive parameters and developers had to use the URI class to access segments and rsegments.

but, at some point (CI 1.5?) this capability was added, but _remap() was not included in the improvement. i think that was an oversight. and clearly, so do you!

perhaps it is worth a "Feature Request" submission?

the main problem addressed by the feature request is that if a developer uses _remap() then they have to rewrite controller functions to get parameters the "old-school" way. or find some other way of getting parameters, like passing the URI segments string (as i showed above).

what do you think?
#9

[eluser]Solito[/eluser]
Hi,


Yes, I think too that _remap should be rewritten :
_remap is the front door of our software architecture and my opinion is that this door is too weak to bear all the functionnalities CI controllers deserve.
Just imagine the security options (or anything else) you could manage before calling any controller, like data validation etc.
Then, the controller would be simpler, meaning a better architecture and more factorization... CI style, what...

But I think there could be a confusion, controller-side speaking.
The controller can have parameters passed in two ways :
1 - as usual, via URI segments
Code:
// for example : a search function
function seek_profile($var1='',$var2='') {
## here, var1 is equal to $this->uri->segment(3) ;
$elts = explode('-',$var1) ; // this is just for the example
## and now treatment
}
2 - via _remap, who can make a treatment on those parameters
Problem is : when you call the controller, which variable will be assigned ?
Code:
// example 2
function _remap($var_a='',$var_b=''){
// here it's var_a that equals this->uri->segment(3)
$var_a = filter_var($var_a,FILTER_VALIDATE_STRING); // example
}

function seek_profile($var1='',$var2){
$elts = explode('-',$var1) ;
## bla bla bla
}
Is var1 equal to segment(3) or to var_a ?
Must _remap override var1 or not ?

Or maybe this can be switchable via a config parameter ? I don't know. I'm only a summer student, you know :coolsmile:
#10

[eluser]sophistry[/eluser]
good analysis and example!

so, maybe the CI manual should say something about this on the _remap page:

"If you use _remap to call controller class methods you have to structure your controllers to handle URI segments by using the URI class accessor methods rather than parameter passing."

anyhow, _remap is only one potential front door for your architecture!

have you considered: routes, hooks, object-oriented class inheritance and overriding?

each of these has overlaps with the others and a thoughtful approach might bring you to a new and better architecture. :-)

cheers.




Theme © iAndrew 2016 - Forum software by © MyBB