Sunday, October 31, 2010

Re: New to MVC

On Sun, Oct 31, 2010 at 9:35 AM, AnotherNetFellow
<anothernetfellow@gmail.com> wrote:
> Hi,
>
> i'm a Python developer, and have worked on small PHP projects. A few
> months ago i've started looking at the MVC model, and have used
> CakePHP for some simple database applications (AddressBook and a
> stupid "crawler").
>
> Now i'm working to an application that uses both Facebook, Youtube and
> Bit.ly APIs. I've read your documentation about the "vendors"
> directory, but am still confused, so i'm asking for help in this
> mailing list. I'll really appreciate any type of help. Thanks. Now let
> me explain:
>
> In my old application I have the bit.ly library that has 2 main
> functions: just_shorten_url and shorten_url. The first function
> creates a short URL and saves it in the db (with an indipendent mysql
> connection) and the first one just creates and print a short url,
> given the long one. In that application i simply include_once the
> library file and then use that functions.
>
> With cakephp, if i'm right, using just_shorten_url is not so
> difficult: just create a view with the form where you can insert the
> long one and a controller where, with my library, the short url gets
> created. Now, what about printing the created url to the user's
> screen? I know I can create a new view where the controller will POST
> data to display, but maybe there is a cleaner way to do this (direct
> output from the controller?). Can you help me?
>
> Using shorten_url (the function that also writes to the db) is more
> difficult: yes, of course i can use it as just_shorten_url and work on
> the database "manually" (the library creates a connection and executes
> queries), but, as i'm using such a powerful framework, why not to use
> it also here? I think the best way to do that is completely remove
> database connections and queries from my library and put everything in
> the controller. The controller gets the long url, with shorten_url
> creates a short one then puts it into the DB.
>
> Well, at this point there is the last problem. At this point i will
> have 2 controllers: one, that creates and output a short url and
> another one, that creates a short url, output it and adds it to the
> DB. So i have almost the same code 2 times in my app. How can i make
> controllers interacting between them? I mean: a controller that
> creates and output the short url that, if needed saves it on the db.

With Cake, one usually wouldn't echo back a simple string unless it
was an AJAX request, in which case you'd use an AJAX layout, which
simply echoes your value (no html, head, body, etc.) and, possibly, an
AJAX view. But that's not a hard and fast rule (I cheat sometimes and
simply echo).

If this isn't an AJAX request, you'd probably be best to redirect
somewhere else and, if you wanted to display the bit.ly-ized URL in
/that/ view, you'd fetch it from the DB in that view's controller
before displaying.

However (and this really depends on what you're trying to do), I think
it'd be simplest to create a component that created the URL. What's
your use case? I mean, would it make sense to provide the URL to be
shortened within some other controller's form? If that were the case,
you could load the component in that controller, pass off the
submitted URL to it, and receive back the shortened version, which
you'd then save with your other data. Unless the only point is to
submit a single URL, and it's not part of some other model's data. If
that's the case, maybe a controller is the correct way forward. But
you wouldn't need 2 controllers. Just create actions for each ...
action you want to provide.

Googling "cakephp bit.ly" shows there are a few existing components
and at least one helper out there. Perhaps you should take a look at
how those were created for ideas (or just use one of them).

Bottom line: creating even one controller for this is overkill if the
shortened URL "belongs" to some other model data. Let that model's
controller deal with it (by way of your component).

I suggest you also have a look around for FB & YT components/plugins
for Cake that others may have created, if only to get a good grounding
in how Cake works. And you could always use those as a basis for
creating your own. Beware, though, that Cake has changed somewhat
since 1.2.x (and A LOT since 1.1.x) so pay close attention to dates,
if any, included with anything you read online, as well as the
information on this page:

http://book.cakephp.org/view/1561/Migrating-from-CakePHP-1-2-to-1-3

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: