letThemCodeCake

Archive for March, 2007

Javascript Helper.

by Ian on Mar.30, 2007, under Learning Cakephp

How does the javascript helper work?

$javascript->link() function.
Youl’d think there’d be a section in the manual under the helpers saying:
Css helper, called by $css->function(). Here’s a list of fuctions.
Javascript helper, called by $javascript->function(). etc.

It looks like the rule of thumb is the full word, lowercase, prefixed with a $ sign, and be sure to add the Helper name in your controller. So to use the javascript ( along with html and form) helper:
I set $var $helpers = (’Html’, ‘Form’, ‘Javascript’); in my controller.

Leave a Comment more...

Advanced Model functions

by Ian on Mar.23, 2007, under Learning Cakephp

Unless you delve in to the API, there are some very useful model functions at your disposal you might not know exist. I highly recommend reading over the Model Class Reference at least once. Here’s a few key functions I wasn’t aware of that I found to be very useful:

  • generateList() - I use this function primarily to populate select boxes with data from associated tables
  • findBySql() - Sometimes you just need to write your own SQL
  • findCount() - Returns number of rows matching given SQL condition
  • hasAny() - Returns true if a record that meets the given conditions exists.

Again, I highly recommend reading over the entire model class reference, you’ll be surprised at what you learn.

Advanced model functions

  powered by clipmarks
Leave a Comment more...

How to use models and controllers in your webpages.

by Ian on Mar.23, 2007, under Learning Cakephp

When first getting started I was quite perplexed as how to build a page that has more than one model and controller attached to it.

If /users/index runs the Users::index() function and the users controller uses the User model then how do I look at data from, say a Profile model on that page? What if I want to have my login function on that page as well?

Well once you get your head around it it’s not that tough, but it took me an embarrassingly long time to get my head around it.

By default the page you go to example.com/users/index is loading the app/views/users/index.thtml file. It’s also running the function index() in app/controllers/users_controller.php and loading the app/models/user.php.

The model tells cake how to interact with the database, and what tables to access.

The controller possibly runs some logic and passes variables to the view.

But the view is in control here. The view loads the html, displays the variables the controller passed (using the set function), and links and forms etc.

So if you want a login form on your index page you simply put that form in your index view and have it submit to /users/login (or whatever function handles the login logic). When you submit that form you’re running that function, not before.

If you want to display information generated (by business logic) in other functions then you can run those functions inside of your index() function. But I think more often you’ll probably want to load data from other models.

I’ll talk about that and the details in my next post.

Leave a Comment more...

How does CSS work in CakePhp?

by Ian on Mar.23, 2007, under Learning Cakephp

To use cakephp’s framework to the fullest, you need to utilize its conventions. To modify the default template for your site copy:

/cake/libs/view/templates/layouts/default.thtml
 to /app/views/layouts/

You then edit that copied file. The presence of

/app/views/layouts/default.thtml

tells cake to stop looking at the old layout and look at the new one.

So you’d think that it would work the same way with css. Well almost. You don’t need to copy the css file, it’s already there for you in

/app/webroot/css/cake.generic.css

Ok great. Just delete that and copy your new css over the old stuff right? No, that’s not a great idea. If you do that you’ll lose all the cool cakephp css classes that you may want to take advantage of later on. For example ‘required’ and ‘optional’ classes for your form labels. Let’s keep the generic css and overwrite it’s classes as we need them.

How do we do that? Let’s go to the default.thtml and we’ll see:

$html->css('cake.generic');

That’s the same as putting

<link rel="stylesheet" type="text/css" 
href="/ProjectPath/css/cake.generic.css" />

In your html code. Well that’s not exactly what we want to use because we want to use the @import url code to import our css files rather than link them.
Luckily the HtmlHelper::css function has an import option. So we use:
$html->css(’cake.custom’, import);

which yields :

@import url(ProjectPath/css/cake.custom.css);

between style tags.
Now that you have an idea of how the html helper css function works you can check out how to auto-load your css files for each controller.

Thanks,
Ian

2 Comments more...

Hi, my name is Ian and I program in the Cakephp framework.

by Ian on Mar.23, 2007, under Learning Cakephp

“Hi Ian”

I’m a new developer that has been happily <a href=”http://bakery.cakephp.org/articles/view/159″>seduced</a> into doing a project or two in the Cakephp framework.

I’m starting this blog because I feel that the learning curve is still a little bit to high, and despite some very good efforts from the cakephp <a href=”http://bakery.cakephp.org/”>community</a> and <a href=”https://www6.software.ibm.com/developerworks/education/os-php-cake1/”>others</a> to welcome newcomers most of the documentation is either in the form of tutorials, the manual, or message boards.

The tutorials that are out there are great but they can only do so much. The manual is still young and needs more content, and as we all know you can only expect so much out of message boards, sometimes they have exactly what you want, sometimes not.

The theme I get from the manual and the message boards is “if you knew how to use cake this would be easy”.  The theme should be “You’re probably very smart, but you’ve got no experience with our way of doing things and you’ve done this your own way many times before, let’s give you the resources  you need to  start programming more effectively”.

This is my first post,  look out for more posts, asking questions, and giving answers on the fundamentals of cakephp.

Thanks,
Ian

2 Comments more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...