polymorphic functions in controller ? |
[eluser]g_montel[/eluser]
Hello Sorry, I'm a newbie who searched in the forum but could not find any solution for that FAQ... I'm using CI 1.7.2. I'd like to have a dynamic image resizing controller that would work this way http://www.mydomain.com/media/img/150/45...size/image -> it would return a 150px wide, 45px high, copy of the image at the server path /local/path/to/fullsize/image I created a Media Controller and an img function inside Code: class Media extends Controller { The problem is I get - obviously - only "150" in vars when I call the controller. I just need a polymorphic function calls for img, so that I could get all the parameters to the img function, whatever the number of parameters following. Is it possible ? Thanks Geoffroy
[eluser]danmontgomery[/eluser]
http://ellislab.com/codeigniter/user-gui...s/uri.html
[eluser]ipavlic[/eluser]
[quote author="g_montel" date="1281469639"]I created a Media Controller and an img function inside Code: class Media extends Controller { The problem is I get - obviously - only "150" in vars when I call the controller. I just need a polymorphic function calls for img, so that I could get all the parameters to the img function, whatever the number of parameters following. Is it possible ? Thanks Geoffroy[/quote] Hi Geoffroy, and welcome to CodeIgniter. noctrum gave you a good answer. If you look at URI Class you can see that you can read any segment by using Code: $this->uri->segment(n) Width is then segment(2), height is segment(3), etc. Using segments you can write a polymorphic function. There is also another, less flexible, option - you can also include more than one parameter in your controller. Code: class Media extends Controller { segment(2) is then mapped to $width, segment(3) to $height, segment(4) to path (but only up until the first slash, the rest is not included). Depending on what you really need, it might be easier to read and write code using the second option.
[eluser]mddd[/eluser]
Or you could use the built-in php function func_get_args() which will give you all the arguments passed to the current function.
[eluser]ipavlic[/eluser]
[quote author="g_montel" date="1281471649"]thanks a lot! sorry for that newbie question...[/quote] Don't worry, we are all learning. You might try checking documentation, it is very well written.
[eluser]Mark Croxton[/eluser]
You might also want to check out this wiki entry. It does exactly what you want: http://codeigniter.com/wiki/Category:Ima...and_Cache/ The code in the wiki entry has a few bugs, so I've attached the version I cleaned up (and I renamed the controller imagesizer since I already had an 'images' directory). Note that the MY_Image_lib mentioned in the wiki is NOT required if you're using 1.7+ |
Welcome Guest, Not a member yet? Register Sign In |