Baking with Cake PHP on Azure Websites

Cake PHP is a MVC framework that makes it easy to quickly build web applications . CakePHP has an inbuilt tool called BAKE that allows you automatically create you controller , view and model files including source code. This article will show you how to use this tool if you have a CakePHP web application running on Azure websites.

  1. Install Cake PHP from the Websites Gallery. Click here to learn how to install the framework.
  2. When a website is created on WAWS, a Kudu website is created to support GIT deployments on WAWS. To access your Kudu site , you need to use the URL in this format https://yoursitename.scm.azurewebsites.net .To learn more see , How to access Kudu Service
  3. You need to update the PATH environment variable to Console folder for using cake command line tool. This step needs to be performed every time a browser session is created for accessing KUDU website that is running on WAWS. To make this easy , create a batch script with the following code

set path=%path%;%home%\site\wwwroot\app\console

 Every time you open a new browser session, run this batch script . This will allow you to run cake console from anywhere.

Now you are all set to run bake scripts to build you simple blog application in minutes. Note to run Bake scripts you must be in the app folder of your website (D:\home\site\wwwroot\app)

Setup your database

Run the following SQL statement to create a Posts table with some sample content

/* First, create our posts table: */

CREATE TABLE posts(  id INT UNSIGNED AUTO_INCREMENT PRIMARYKEYtitle VARCHAR(50), body TEXT,  created DATETIME DEFAULT NULLmodified DATETIME DEFAULT NULL);

/* Then insert some posts for testing: */

INSERT INTO posts(title,body,createdVALUES(‘The title’,‘This is the post body.’,NOW());

INSERT INTO posts(title,body,createdVALUES(‘A title once again’,‘And the post body follows.’,NOW());

INSERT INTO posts(title,body,createdVALUES(‘Title strikes back’,‘This is really exciting! Not.’,NOW());

Create a Model

Models are the classes that are responsible for managing almost everything that happens regarding your data, its validity, interactions and evolution of the information workflow in your domain of work. Now we are going to setup the Model, create a new file in the wwwroot\app\Model directory and name it “post.php” and enter the following:

<?php

class Post extends AppModel {

var $name =‘Post’;

}

?>

Save the file and now access the Debug Console in your Kudu website.

Create the controller

Run cake bake command under the app folder

Select C option to create a controller. For database config , select default as the value to choose .

 

This will list out the possible controllers based on your database and follow the wizard to create controller

imagecontroller

Now your Posts Controller has been created and the source code has been written for you. Open up the app\controller\PostsController.php file , you will see that the The basic functions have been included in the file that correspond to actions that you would want the application to do.

  • index(), will get all the posts and send them to your index view
  • view(), will access a single post
  • add(), programming logic to add a new post to the database
  • edit(), will edit a single post
  • delete(), will remove a post from the database

Create a view

Next we are going to bake the views for this application. Run the bake script again in the command prompt and this time choose to bake a View by entering “V” when asked. Similar to when baking the controller you will be given a list of items, again choose “Posts” and follow the wizard. Skip the views for admin routing and it should create the views and all the code automatically.

imageview

During the interactive wizard , you need to configure routing prefixes in app/Config/core.php to make sure routing is configured for all the views add,delete,index,view. As you see in the image above , I choose the default admin as the prefix. Have a look in the app\view\posts folder where you will find a file for every controller method you will have a file for each of your controller methods excluding delete (this is because delete does not use a view it just redirects automatically). To learn more about View, see this article.

Wrapping up

Run the site now to view all the posts from the database visible http://cakebakesite.azurewebsites.net/posts/

 Update the app\config\routes.php file to include this code snippet :

    //comment out the line below    
    //Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));         
        
    Router::connect('/',array('controller'=>'posts','action'=>'index'));    

Well that’s ‘it! You have a simple blog application running on WAWS using bake in just a few minutes. This was a simple example, you can build complex applications using Cake PHP and Bake scripts.

Additional resources

Code generation with Bake

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s