Using Homestead with CakePHP Framework

Joe • June 30, 2017

cakephp vagrant

Interested in checking out CakePHP? You can easily spin up a new CakePHP project and add Homestead just as easy as any other modern PHP project.

When I teach Laravel I use a quick start style application. I'll be starting to build this same application with CakePHP:


composer create-project -- prefer-dist cakephp/app cakephp-quickstart
cd cakephp-quickstart
composer require -- dev laravel/homestead
./vendor/bin/homestead make

CakePHP does not use the traditional `public` folder that other frameworks use as the HTTP entry point into their application. If we look at our newly generated `Homestead.yaml` file:


ip: 192.168.10.10
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
keys:
- ~/.ssh/id_rsa
folders:
-
map: /Users/halo/PhpstormProjects/cakephp-quickstart
to: /home/vagrant/cakephp-quickstart
sites:
-
map: cakephp-quickstart.app
to: /home/vagrant/cakephp-quickstart/webroot
databases:
- homestead
name: cakephp-quickstart
hostname: cakephp-quickstart

As you can see on our sites mapping we need to map the site to the `webroot` path since this is the HTTP entry point for CakePHP.

Now we're ready to run `vagrant up` like we normally would:


vagrant up
Bringing machine 'cakephp-quickstart' up with 'virtualbox' provider...
==> cakephp-quickstart: Importing base box 'laravel/homestead'...
==> cakephp-quickstart: Matching MAC address for NAT networking...
==> cakephp-quickstart: Checking if box 'laravel/homestead' is up to date...
==> cakephp-quickstart: Setting the name of the VM: cakephp-quickstart
==> cakephp-quickstart: Clearing any previously set network interfaces...
==> cakephp-quickstart: Preparing network interfaces based on configuration...
    cakephp-quickstart: Adapter 1: nat
    cakephp-quickstart: Adapter 2: hostonly
==> cakephp-quickstart: Forwarding ports...
    cakephp-quickstart: 80 (guest) => 8000 (host) (adapter 1)
    cakephp-quickstart: 443 (guest) => 44300 (host) (adapter 1)
    cakephp-quickstart: 3306 (guest) => 33060 (host) (adapter 1)
    cakephp-quickstart: 5432 (guest) => 54320 (host) (adapter 1)
    cakephp-quickstart: 8025 (guest) => 8025 (host) (adapter 1)
    cakephp-quickstart: 27017 (guest) => 27017 (host) (adapter 1)
    cakephp-quickstart: 22 (guest) => 2222 (host) (adapter 1)
...
==> cakephp-quickstart: Running provisioner: shell...
    cakephp-quickstart: Running: inline script
==> cakephp-quickstart: Running provisioner: shell...
    cakephp-quickstart: Running: /var/folders/dv/jhkxknyj3nscz4hlghs_45280000gn/T/vagrant-shell20170630-4404-u9meue.sh
==> cakephp-quickstart: Running provisioner: shell...
    cakephp-quickstart: Running: script: Creating Certificate: cakephp-quickstart.app
==> cakephp-quickstart: Running provisioner: shell...
    cakephp-quickstart: Running: script: Creating Site: cakephp-quickstart.app
==> cakephp-quickstart: Running provisioner: shell...
    cakephp-quickstart: Running: script: Checking for old Schedule
==> cakephp-quickstart: Running provisioner: shell...
    cakephp-quickstart: Running: script: Restarting Nginx
==> cakephp-quickstart: Running provisioner: shell...
    cakephp-quickstart: Running: script: Creating MySQL Database: homestead
==> cakephp-quickstart: Running provisioner: shell...
    cakephp-quickstart: Running: script: Creating Postgres Database: homestead
==> cakephp-quickstart: Running provisioner: shell...
    cakephp-quickstart: Running: script: Clear Variables
==> cakephp-quickstart: Running provisioner: shell...
    cakephp-quickstart: Running: script: Update Composer
==> cakephp-quickstart: Updating to version 1.4.2 (stable channel).
==> cakephp-quickstart:
==> cakephp-quickstart: Downloading (connecting...)
==> cakephp-quickstart:
==> cakephp-quickstart: Downloading (100%)
==> cakephp-quickstart:
==> cakephp-quickstart:
==> cakephp-quickstart: Use composer self-update -- rollback to return to version 1.4.1
==> cakephp-quickstart: Running provisioner: shell...
    cakephp-quickstart: Running: /var/folders/dv/jhkxknyj3nscz4hlghs_45280000gn/T/vagrant-shell20170630-4404-3ae3f1.sh

Now we can view the site in our browser at http://localhost:8000 and we should see the following:

If you look closely you will see a Database error "CakePHP is NOT able to connect to the database. Connection to database could not be established: SQLSTATE[HY000] [1045] Access denied for user ‘my_app'@'localhost' (using password: YES)"

CakePHP stores the database information in the file `config/app.php` in the Datasources.default array:


'username' => 'my_app',
'password' => 'secret',
'database' => 'my_app',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,

Changing the `username` and `database` fields to `homestead` (which is default for the Homestead environment) will resolve the database connection error.

Don't like the `webroot` folder and want to use `public` instead? CakePHP makes this easy. Simply rename the folder in your project, go back to the file `config/app.php` and look for the App.webroot array key and change `webroot` to `public`, and then update your `Homestead.yaml` file to point the site back to `public` and run `vagrant destroy && vagrant up`. Now your project is being served from the `public` folder.

I hope you found this useful, thanks for reading.