Thursday, August 11, 2011

cake php

  • Before read this i think you have some programming knowledge and client server architecture. 
  • What is Cake PHP:
    • a quick list of features you’ll enjoy when using CakePHP:
    • Active, friendly community
    • Flexible licensing
    • Compatible with versions 4 and 5 of PHP
    • Integrated CRUD for database interaction
    • Application scaffolding
    • Code generation
    • MVC architecture
    • Request dispatcher with clean, custom URLs and routes
    • Built-in validation
    • Fast and flexible templating (PHP syntax, with helpers)
    • View Helpers for AJAX, JavaScript, HTML Forms and more
    • Email, Cookie, Security, Session, and Request Handling Components
    • Flexible ACL
    • Data Sanitization
    • Flexible Caching
    • Localization
    • Works from any web site directory, with little to no Apache configuration involved
  • Requriment for cake php:
    • Requirements

      • HTTP Server. For example: Apache. mod_rewrite is preferred, but by no means required.
      • PHP 4.3.2 or greater. Yes, CakePHP works great on PHP 4 and 5.



      CakePHP supports a variety of database storage engines:
      • MySQL (4 or greater)
      • PostgreSQL
      • Microsoft SQL Server
      • Oracle
      • SQLite

  • It is a MVC based php framework for rapid application development.
  • Used to build a rapidly develop robust web applications, without any loss to flexibility.
Now what is MVC in very easy and static way i explain in 6 step:


  • See the picture above it helps you a lot:
    • step 1 : User send a request for page.
    • step 2 : Request goes to server's controller.
    • step 3 : Controller find the specific page in model(also called a database table , rss feed or any other file) .
    • step 4 : If found come back to controller and after that controller.
    • step 5 : Then controller goes to the view, it is the view who represent the data in the form of html or pdf or other way.
    • step 6 : At last it is the view that transfer to the client back.
  • This is all over MVC hope you got it... 
  • here the file structure of cake php  :
    • the Root folder contain:
      • /app
      • /cake
      • /plugins
      • /vendors
      • .htaccess
      • index.php
      • README
  • The app folder will be where you work your Programming Code files: it’s where your application’s files will be placed.
  • The cake folder contained very useful predefined data files. make sure that you never  edit files in this folder. This is the core of Cake .
  • Finally, the vendors folder is where you’ll place third-party PHP libraries you need to use with your CakePHP applications.

The App Folder

CakePHP’s app folder is where you will do most of your application development. Let’s look a little closer at the folders inside of app.

config : Holds the (few) configuration files CakePHP uses. Database connection details, bootstrapping, core configuration files and more should be stored here.
controllers :Contains your application’s controllers and their components.
libs: Contains 1st party libraries that do not come from 3rd parties or
external vendors. This allows you to separate your organization's internal libraries from vendor libraries.
locale: Stores string files for internationalization.
models: Contains your application’s models, behaviors, and datasources.
plugins: Contains plugin package.
tmp: This is where CakePHP stores temporary data. The actual data it stores depends on how you have CakePHP configured, but this folder is usually used to store model descriptions, logs, and sometimes session information.

Make sure that this folder exists and that it is writable, otherwise the performance of your application will be severely impacted. In debug mode, CakePHP will warn you if it is not the case.
vendors: Any third-party classes or libraries should be placed here. Doing so makes them easy to access using the App::import('vendor', 'name') function. Keen observers will note that this seems redundant, as there is also a vendors folder at the top level of our directory structure. We'll get into the differences between the two when we discuss managing multiple applications and more complex system setups.
views: Presentational files are placed here: elements, error pages, helpers, layouts, and view files.
webroot: In a production setup, this folder should serve as the document root for your application. Folders here also serve as holding places for CSS stylesheets, images, and JavaScript files.


Another important point is CakePhp Convension:

CakePHP Conventions:

  • It will take a time to learn CakePHP’s conventions.
  • But this will helps you save time in the long run.
  • By the following convention you able to do:
    • the maintenance nightmare of tracking config files.
    • Convention also makes for a very uniform system development.
    • Allowing other developers to jump in and help more easily.

File and Classname Conventions:

 

In general, filenames are underscored while classnames are CamelCased. So if you have a class MyNiftyClass, then in Cake, the file should be named my_nifty_class.php. Below are examples of how to name the file for each of the different types of classes you would typically use in a CakePHP application:
  • The Controller class KissesAndLovesController would be found in a file named kisses_and_loves_controller.php (notice _controller in the filename)
  • The Component class MyFutureComponent would be found in a file named my_future.php
  • The Model class OptionValue would be found in a file named option_value.php
  • The Behavior class MyNewBehavior would be found in a file named my_new.php
  • The View class SuperSimpleView would be found in a file named super_simple.php
  • The Helper class BestEverHelper would be found in a file named best_ever.php

Each file would be located in or under (can be in a subfolder) the appropriate folder in your app folder.
--------------------------------------------------------------------------------

Model and Database Conventions:

  1. Model classnames are singular and CamelCased. Person, BigPerson, and ReallyBigPerson are all examples of conventional model names.

  • Table names corresponding to CakePHP models are plural and underscored.
  • The underlying tables for the above mentioned models would be people, big_people, and really_big_people, respectively.
  • Field names with two or more words are underscored like, first_name.

 

Controller Conventions:

Controller classnames are plural, CamelCased, and end in Controller. PeopleController and LatestArticlesController are both examples of conventional controller names.

C.T.M: 

  • The first method you write for a controller might be the index() method.
    When a request specifies a controller but not an action, the default CakePHP behavior is to execute the index() method of that controller. 
    For example, a request for http://www.example.com/apples/ maps to a call on the index() method of the ApplesController, whereas http://www.example.com/apples/view/ maps to a call on the view() method of the ApplesController.

 

URL Considerations for Controller Names:

As you've just seen, single word controllers map easily to a simple lower case URL path. For example, ApplesController (which would be defined in the file name 'apples_controller.php') is accessed from http://example.com/apples.
i keep updating this :   stay tuned.... thanks ..


View Conventions:

  • View template files are named after the controller functions they display, in an underscored form. 
  • The getReady() function of the PeopleController class will look for a view template in /app/views/people/get_ready.ctp. 
  • The basic pattern is /app/views/controller/underscored_function_name.ctp.
Here’s a final example that ties the conventions
  • Database table: "people"
  • Model class: "Person", found at /app/models/person.php
  • Controller class: "PeopleController", found at /app/controllers/people_controller.php
  • View tem
  • plate, found at /app/views/people/index.ctp
-------------------------------------------------------------------
Let us start some delelopment:
The next big thing you have to done is:

Creating the Blog Database:

if you haven't already done this, create an empty database for use in this tutorial, with a name asn you wish.

  • create table name posts

    1. /* First, create our posts table: */
    2. CREATE TABLE posts (
    3. id INT AUTO_INCREMENT PRIMARY KEY,
    4. title VARCHAR(50),
    5. body TEXT,
    6. created DATETIME DEFAULT NULL,
    7. modified DATETIME DEFAULT NULL
    8. );
    9. /* Then insert some posts data  for testing: */
      • INSERT INTO posts (title,body,created)
      • VALUES ('The title', 'This is the post body.', NOW());
      •  
      • INSERT INTO posts (title,body,created)
      • VALUES ('A title once again', 'And the post body follows.', NOW());
      •  
      • INSERT INTO posts (title,body,created)
      • VALUES ('Title strikes back', 'This is really exciting! Not.', NOW());
 After that:


Controller in brief:-

  • if you were building a site for an online examsystem, you might have a OnlineexamController .
  • In CakePHP, controllers are named after the model they handle, in plural form. 
  • So the The OnlineExam model is handled by the OnlineexamController and so on. 
  • Imp Point remember:
    • Your application's controllers are classes that extend the CakePHP AppController class, which in turn extends a core Controller class, which are part of the CakePHP library. 
    • The AppController class can be defined in /app/app_controller.php and it should contain methods that are shared between all of your application’s controllers.
  • Controllers can include any number of methods called as actions. 
  • Actions are controller methods used to display views. An action is a single method of a controller.
   
<?    class OnlinexamController extends AppController {
        function view($id)     {
            //action logic goes here..
        }

        function share($student_id, $stuname_id) {
            //action logic goes here..
        }

        function search($query) {
            //action logic goes here..
        }
    }

    ?>

-------------------------------------------------------------------



Source CakePhp.org