XAMPP-VM for OS X is a XAMPP application that lets you run XAMPP for Linux on your Mac using an OS X hypervisor based on hyperkit. As a result, you have a dev environment that's much closer to what you'll usually see in production. Plus, it's very lightweight, as it doesn't require other preinstalled software like VirtualBox or VMware.
Putting together a database-backed CakePHP application on XAMPP-VM using the included Apache, PHP and MariaDB components is fairly easy to do. This blog post will walk you through the steps.
- You've already installed and started XAMPP-VM
- You have some familiarity with PHP development, including how to create tables and grant user privileges in MariaDB (learn more)
Before proceeding, check the XAMPP-VM stack manager and ensure that:
- The Apache and MySQL services are running.
- Port forwarding between port 8080 of the host machine and port 80 of the XAMPP-VM stack is enabled.
- The /opt/lampp directory of the XAMPP-VM stack is mounted.
Create a skeleton CakePHP application
The easiest way to get started with a new CakePHP application is with Composer, the PHP dependency manager. From the XAMPP-VM stack manager's "General" tab, click "Open Terminal". This will start a new XAMPP-VM console with root privileges. At the console prompt, execute the command below to download Composer:
$ curl -s https://getcomposer.org/installer | php
Once Composer has been downloaded, execute the command below to create a new CakePHP skeleton application in the /opt/lampp/htdocs/myapp directory:
$ COMPOSER_ALLOW_SUPERUSER=1 php composer.phar create-project --prefer-dist cakephp/app myapp
|NOTE: Composer will not allow you to run commands as root for security reasons and will normally issue a warning when you attempt this. However, since the XAMPP-VM console is preconfigured to run with root privileges, the COMPOSER_ALLOW_SUPERUSER=1 environment variable is added to the previous command to disable the warning. Learn more about this in the Composer documentation.|
You should see something like this as the skeleton application is installed:
Once the skeleton application is installed, test it by browsing to http://localhost:8080/myapp. You should see the default CakePHP application welcome page, as shown below:
Notice that the page includes an error about CakePHP being unable to connect to the database. We'll fix that in the next step. For the moment, rejoice in the fact that your CakePHP skeleton is installed and (mostly) ready for use.
Create a database user and database
XAMPP-VM includes phpMyAdmin, which you can access by browsing to http://localhost:8080/phpmyadmin (remember that port forwarding should be active for this to work). From the "User accounts" tab, create a new user called myapp, set a password and instruct phpMyAdmin to create a database of the same name and grant this user account full privileges to it.
Switch back to the XAMPP-VM console and adjust the default ownership of the /opt/lampp/htdocs/myapp/config/app.php file so that you can edit it through the OS X Finder:
$ cd /opt/lampp/htdocs/myapp $ chown bitnami.root config/myapp.php
Using the OS X Finder, browse to the mounted /opt/lampp directory and edit the htdocs/myapp/config/app.php file. Update the "Datasources" key of the configuration array with the new database name and account credentials created above, as shown below, and save the changes.
Re-test the skeleton application by browsing to http://localhost:8080/myapp. You should now see that the previous database connection error has been resolved and the CakePHP application is able to connect to the XAMPP-VM MariaDB database using the provided credentials.
You're now ready to begin developing the application.
Create a database table and CRUD scaffolding
We won't be developing anything very complicated here; a simple to-do list application should provide the foundation you need to start creating your own applications. Begin by browsing to phpMyAdmin and creating a new table named todo in the myapp database with three fields: a primary key (id), a VARCHAR field (title) and a BOOLEAN field (status). Here's what the result should look like:
One of the nice things about CakePHP is that it can inspect your database and automatically create basic Create/Read/Update/Delete (CRUD) scaffolding to interact with your data. This significantly simplifies the amount of work you have to do when building database-driven apps. To see this in action, switch back to the XAMPP-VM console and run the following commands:
$ cd /opt/lampp/htdocs/myapp $ chmod +x bin/cake $ ./bin/cake bake all todo
CakePHP will go to work creating the necessary CRUD templates and code for the todo table. Once it's done, browse to http://localhost:8080/myapp/todo and you should see a basic interface to add, edit and delete todo list items, as shown below:
Try adding a new item, and you'll see it appear in the list, together with links to view, edit and delete it:
As you can see, you've now got the bare bones of a database-driven PHP application running on XAMPP-VM…and it was fairly easy to get here as well!
You can now continue building out the application by adding more database tables, modifying the templates to use a different interface, or adding custom business logic to the controllers. Read more about all these tasks in the official CakePHP cookbook, or download XAMPP-VM for OS X and try it today!