Welcome Guest, Not a member yet? Register   Sign In
polymorphic functions in controller ?
#1

[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 {
    function img($vars)
    {
        // parse vars to get width, height, and local path
    }
}

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
#2

[eluser]danmontgomery[/eluser]
http://ellislab.com/codeigniter/user-gui...s/uri.html
#3

[eluser]ipavlic[/eluser]
[quote author="g_montel" date="1281469639"]I created a Media Controller and an img function inside
Code:
class Media extends Controller {
    function img($vars)
    {
        // parse vars to get width, height, and local path
    }
}

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)
Segments are separated by a slash '/', and start at your controller.
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 {
    function img($width, $height, $path)
    {
        
    }
}

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

[eluser]g_montel[/eluser]
thanks a lot! sorry for that newbie question...
#5

[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.
#6

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

[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+




Theme © iAndrew 2016 - Forum software by © MyBB