Sunday, October 31, 2010

Re: New to MVC

On Sun, Oct 31, 2010 at 6:18 PM, Giorgio <anothernetfellow@gmail.com> wrote:
> Thankyou Cricket for your kind reply! I'll try to give more details about
> the part of my application i'm trying to rewrite with cakephp.
>
> 1 - In a form, an user inserts a youtube link and click on submit
> 2 - My application handles the input and
> 2.1 - Gets, from youtube api, details of that video and stores them in a db
> (get_video_details() + a connection to the DB to store data)
> 2.2 - Creates a short url and saves it into the db (shorten_url() + a
> connection to the db to store data)
> 3 - Outputs a confirmation page with the video embedded in it

So creating the Bit.ly link is just one part of the request. In that
case, go with a component. Save the the shortened URL the component
returns to the controller into the DB and include it with whatever
else you're responding back to the client.

> As you can see, this is very simple. I just need to understand where i have
> to put those "workers", those functions.

It looks like both the YT and Bit.ly stuff should be handled by a
component (but more on that below).

> Do you confirm that a component is a library in the Vendors directory?

No. A component is a class that your controller uses. They can either
be inside the app/controllers/components or the
app/plugin/your_plugin/controllers/components dirs. The latter would
be for a component that is used solely for that plugin, whereas the
former could be loaded by any of your other app's controllers. An
example of that would be your own EmailComponent, an image handler,
submitted html content cleaning, etc. Anything that doesn't require a
full-blown plugin (eg. needs a model, controller, component, etc.) but
that could be used by one or more of your controllers is a good
candidate for a component.

Vendors, OTOH, are classes, or collection of classes, that are not
written with the Cake framework. Things like the YouTube API,
PHPThumb, etc. The way that you load them is with with App::import().
To use a vendor package with your own component you'd do something
like:

-- snip --
App::import('Vendor','HTMLPurifier'
,array('file'=>'htmlpurifier'.DS.'library'.DS.'HTMLPurifier.auto.php'));

class PurifierComponent extends Object
{
-- snip --

Basically, you want to load whatever class is necessary to use the
package. Here, the component acts a wrapper of, or interface to, the
vendor package. So you'd include some methods for accessing the vendor
class' methods with your controller.

> And,
> sorry, i was using the wrong words in the first post. I didn't want to
> create a controller to substitute my 2 functions (shorten_url and
> just_shorten_url), but 2 actions. Right now, the shorten_url function calls
> just_shorten_url, gets the shortened url and puts them in the db. I have a
> basic function, that makes api calls and returns a shorten url and another
> function that uses this basic functions and does more things on its output.
> Can i have something like this with actions?

You certainly could do that. But I think it'd be better to use a
component and have that return the result tothe controller, which
would then save to the DB and set vars for the view. So you could do
something like:

$shortened = $this->Bitly->shorten($this->data['YourModel']['url'];

Where $this->Bitly refers to your own BitlyComponent.

I hope that makes sense. I gotta go stir my chili!

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: