Twitter Delicious Facebook Digg Stumbleupon Favorites More

Tampilkan postingan dengan label CakePhp. Tampilkan semua postingan
Tampilkan postingan dengan label CakePhp. Tampilkan semua postingan

Jumat, 10 April 2009

Models, Views, and Controllers (MVC)

Cake uses the MVC structure as it compiles. What this means is that it e"ectively separates typi-
cal operations into speci!c areas: models for all your database interaction; views for all your out-
put and displays; and controllers for all your commands/scripts.
MVC can vary depending on the framework with which you’re working. But generally it works
like this.

  1. e user interacts with the interface in some way (e.g., presses a button).
  2. E controller handles the input event.
  3. e controller accesses the model to fetch any saved data or fetch data stored in the data-base.
  4. view uses data handled by the controller and model to generate the user interface orany other output that must be sent to the user.
  5. e interface waits for further user interaction before the cycle begins anew.

e bene!t of using MVC structures in developing websites is that repeated functions or tasks can be separated, thus allowing for quicker edits. It can even help in debugging. Say an error keeps occuring during the interaction with the database. Usually the problem will be somewhere in a model, or least likely in the controller (if you follow convention). Knowing that all database interactions occur in just one place makes addressing problems more e$cient.
Cake makes MVC developing a snap and actually has a straightforward folder structure that cuts down on nested folders and extra directories. When deciding between other frameworks, realize that Cake has a more e$cient MVC structure than Symfony or Zend and will occupy less space on your server.

THE NEWBIE’S INTRODUCTION TO CAKEPHP

WHY CAKEPHP?
Ever since the boys at 37 Signals (www.37signals.com) released Ruby on Rails as an open source
project, the world of web development has been moving more toward a framework-based solu-
tion for creating dynamic websites. Gone are the days of individually scripting each and every
function the website will utilize. Instead, developers are using a bundled package of scripts that
perform a series of functions designed speci!cally for web development.
Because Rails set the standard for web-based frameworks, teams of developers have been creating
clones of Rails for various languages: Turbogears for Python; Symfony, PHP on Trax, Zend and
many others for PHP; and Catalyst for Perl—and on and on. With so many options out there,
why choose CakePHP (or, for short, Cake) for your web project?

Sabtu, 04 April 2009

Editing the database configuration file

Setting up your database configuration is pretty darn easy, but before you start,
make sure your database server is running, that you have created a database for
your application, and that you have a username and password for a user with rights
to the database.
To start, make a copy of the app/config/database.php.default file and save it as
app/config/database.php. Do this to preserve a copy of the original template. Open
the file in your favorite text editor and look for the following section (it should be just
near the bottom of the file)



Configuring a database connection cakephp

Now that you've got CakePHP installed and on speaking terms with your Web
server, you need to introduce CakePHP to your database. This section will cover
setting up the database configuration and verifying that CakePHP likes your
database. Tor is going to need a place to store its user and product data. You'll be
soon be creating a users table to be used to build the login and registration parts of
Tor.




Configuring a database connection cakephp

Now that you've got CakePHP installed and on speaking terms with your Web
server, you need to introduce CakePHP to your database. This section will cover
setting up the database configuration and verifying that CakePHP likes your
database. Tor is going to need a place to store its user and product data. You'll be
soon be creating a users table to be used to build the login and registration parts of
Tor.




Installation

CakePHP wants to make your life easier, regardless of your level of experience, by
making your applications easier to maintain and quicker to write. CakePHP is full of
cool and useful features. CakePHP wants to handle your Ajax, your data validation,
your sessions. It will even slice your bread if you can write a plug-into tell it how. But
you can't use CakePHP yet. You need to install it first





Sabtu, 21 Maret 2009

open_basedir will slow you down - security vs. speed

I’m working on some security enhancements for my new website project, and in that progress, I have moved away from Apache2.2.3 and now using Lighttpd instead. Lighttpd does not use the same method of invoking php as apache does (mod_php vs. fast-cgi php).

In my eager to make the application as secure and locked down as possible, I went berserk in the php-cgi.ini file and turned on just about any security feature I could find (not safemode!), including the innocent looking switch called “open_basedir”.

That was rather late in the night, so when I resumed my work the following day, I have long forgotten about all the fancy security settings I had enabled in php.

A few hours ago I noticed that the complete render time for the front page suddenly was ~2seconds(!) - and comparing with apache’s load time for the same page (0.9s) I was quite disappointed at lighttpd and fastcgi, but refused to give up my new found love without a fair trial.

I attacked google with full force without finding any useful resources that might be able to explain my issue.. I was quite sure it was APC that did not function well in a fastcgi environment - and therefore my google madness was focused around the keywords “apc + fastcgi”

While chatting to PatrickDK in the #lighttpd channel @ freenode, it suddenly hit me - let’s try to disable everything I have “fixed” last night - starting with XDebug, some 3rd party php modules (syck, memcache, simplexml, fileinfo) - and then, open_basedir flag.

And with just one line commented out in my php-cgi.ini file, the render time went from 2.4 to 0.5 seconds !!(!!)

So just a little note to you guys out there, beware of the open_basedir setting in a setup where you include many files, it is a rather costly feature to use. And when running fastcgi php in chroot()ed setup, its not really required anyway

Its another +1 for lighty and fastcgi

And yes, I’m aware that open_basedir is slow because it has to validate EVERY file or path you work with inside php to check if its within the scope defined.



Tutorial Cake Php

PHP doc blcok

This function can be thought of as a hybrid between PHP's array_merge and
array_merge_recursive. The difference to the two is that if an array key
contains another array then the function behaves recursive (unlike array_merge)
but does not do if for keys containing strings (unlike array_merge_recursive).
See the unit test for more information.

Note: This function will work with an unlimited amount of arguments and
typecasts non-array parameters into arrays.

A bit like array_merge_recursive. Merge joins two arrays on their keys.
If we want to add the $extraData array into the $dat most people would probably think, hey, lets merge them using Set::merge… but.. no, dont, unless you know what your doing.



test data
$data = array(
1 => array('Post' => array(
'id' => 1, 'post_comment_count' => 2,
'name' => 'My first title', 'body' => 'My first body'
),
'PostComment' => array(
array(
'id' => 1, 'post_id' => 1, 'is_active' => 1,
'name' => 'My first comment', 'body' => 'My first comment'),
array(
'id' => 2, 'post_id' => 2, 'is_active' => 1,
'name' => 'My second comment', 'body' => 'My second comment'),
array(
'id' => 3, 'post_id' => 2, 'is_active' => 1,
'name' => 'My third comment', 'body' => 'My third comment')
)
),
2 => array('Post' => array(
'id' => 2, 'post_comment_count' => 1,
'name' => 'My second title', 'body' => 'My second title'
),
'PostComment' => array(
array(
'id' => 4, 'post_id' => 2, 'is_active' =>
1, 'name' => 'My fourth comment', 'body' => 'My fourth comment'
)
)
),
3 => array('Post' => array(
'id' => 3, 'post_comment_count' => 0,
'name' => 'My third title', 'body' => 'My third title'
),
'PostComment' => array(

)
)
);

$dataExtra = array(
4 => array('Post' => array(
'id' => 4, 'post_comment_count' => 1,
'name' => 'My fourth title', 'body' => 'My fourth title'
),
'PostComment' => array(
array(
'id' => 5, 'post_id' => 4, 'is_active' => 1,
'name' => 'My fifth comment', 'bdoy' => 'My fifth comment'
)
)
)
);

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Blogger Templates