Connecting CakePHP 3 To A Database

This quick guide will continue from this setup tutorial, and show you the simple edit you need to make to your CakePHP ‘config.php’ file to connect to you’re database.

This of course will work with any mysql setup, but since we setup our installation on a Digital Ocean Ubuntu server using the PHPMyAdmin one click setup I’ll point out where you can find your mysql user and password info while we’re here.

When you login to you’re server via terminal:

You should be given a welcome message and some info about your setup. You should be able to see something along the lines of the following:

  • The URL to access PHPMyAdmin will be: http://YOUR_IP/phpmyadmin
  • Your user will be: admin
  • Your password will be: Whatever is displayed above

Also not the last line in the above image. There is a ‘.digitalocean_password’ file created in the ‘/root’ directory of your server. If you open that file in a text editor you’ll see that your mysql installation has a few users already configured, one of which will be the same as the user above.

Step 1: Create a Database

First you need you’ll need to login to PHPMyAdmin and create a database for your CakePHP application to use. Once you’ve logged in create a new database by clicking ‘New‘ on the top of the menu on the left of your screen (below):

 

Give your database a name (lowercase, no spaces…obviously), hit ‘Create’, and you’re done here.

Step 2: Update the Config File

You need to connect your CakePHP config file to your new database. The easiest way to do this is to login to your server with FTP software (Filezilla for example).

Once you’re connected navigate to the directory path: /var/www/html/YOUR_APP_NAME/config

In this directory you’ll find an ‘app.php’. This file contains a lot of the general setting for your application, it’s extremely well commented. For now you need to tell it what database to use and the user and password for said database. At the time of writing this in the latest version of CakePHP they are lines 232, 233 and 234. Update the placeholder info with the relevant data and save the file. Upload the new version of that file overwriting the old one, and were done. If you return to the default home page of your application (your IP address if you’ve followed from this setup tutorial), you should now see that your application can talk to your new database.

Time to start making stuff!

 

Installing Firebase with CocoaPods

Installing Firebase with CocoaPods is much like installing any other pod.

First you’ll need a Podfile. If you’re integrating Firebase into a project that already uses libraries from CocoaPods, obviously you can use the existing file.

Fire up a new terminal window and navigate to the folder where your project is located. Mine is on my desktop in a folder called ‘FirebaseDemo’, so my terminal command will look like this:

Next you can create a new Podfile:

Open that file in a text editor of choice (do not use TextEdit) and you should see something like this

The few instances of ‘FirebaseDemo’ will obviously be replaced with the name of your Xcode project. Lines starting with a hashtag are commented out. I’ll remove them for clarity, you can do the same of leave them as they are.

Replace the line ‘# Pods for FirebaseDemo’  with the following:

As you can see the Firebase library is broken into parts. It’s a substantial library, and you may not need all of it, this gives you flexibility to only include the parts that you want. Including the Core, Database, and Auth component is probably a fairly common setup to get started.

Finally, back in terminal (making sure your still in the correct folder)  run:

CocoaPods will go and install the relevant dependancies you’ll see something like the following:

The important part of this, as with all projects using Cocoapods is that you need to open the .xcworkspace (the white icon) file in Xcode from now on.

Using Firebase in your project requires that you setup a project in the Firebase console and “link” it with an app. For a guide on that process can be found here.