I have been going through the CakePHP Bookmarker Tutorial, and have struggled with getting it to work following the instructions outlined on those pages. In this blog, i will outline what steps I need to follow in order to get this to work.
My environment is as follows:
- CentOS 6 virtual machine (on VirtualBox)
- PHP 7.0 (php70u) with Apache 2.2.15 with mod_rewrite
To install CakePHP you will need Composer and also ext-intl. On CentOS, ext-intl is installed using `yum install php70u-intl`.
To install the project, run this command:
composer self-update && composer create-project --prefer-dist cakephp/app bookmarks
Notice I’ve used a project name of “bookmarks” instead of “bookmarker”.
Creating the Database
To create the database you’ll need to login to MySQL (presumes MySQL is used) and then create the specific database used. Let’s call it “cakedb”. Here is the sequence of commands:
mysql -u username -p Enter password: userpassword MariaDB [(none)]> CREATE DATABASE cakedb; MariaDB [(none)]> USE cakedb;
ALTER DATABASE cakedb CHARACTER SET utf8;
In the above commands, “username” is the MySQL username that can login and create databases. I had to set the character set to UTF-8 so that when creating the tags table, the length of “title VARCHAR(255)” won’t throw an error (ERROR 1071) about the maximum key length.
Run these SQL commands to create the database tables:
CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, email VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, created DATETIME, modified DATETIME ); CREATE TABLE bookmarks ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL, title VARCHAR(50), description TEXT, url TEXT, created DATETIME, modified DATETIME, FOREIGN KEY user_key (user_id) REFERENCES users(id) ); CREATE TABLE tags ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255), created DATETIME, modified DATETIME, UNIQUE KEY (title) ); CREATE TABLE bookmarks_tags ( bookmark_id INT NOT NULL, tag_id INT NOT NULL, PRIMARY KEY (bookmark_id, tag_id), FOREIGN KEY tag_key(tag_id) REFERENCES tags(id), FOREIGN KEY bookmark_key (bookmark_id) REFERENCES bookmarks(id) );
After creating the tables, exit MySQL:
MariaDB [cakedb]> exit;
Modify Config File
Open the file “config/app.php” and set the database username and password, and database name:
'Datasources' => [ 'default' => [ 'className' => 'Cake\Database\Connection', 'driver' => 'Cake\Database\Driver\Mysql', 'persistent' => false, 'host' => 'localhost', /** * CakePHP will use the default DB port based on the driver selected * MySQL on MAMP uses port 8889, MAMP users will want to uncomment * the following line and set the port accordingly */ //'port' => 'non_standard_port_number', 'username' => 'username', 'password' => 'userpassword', 'database' => 'cakedb', 'encoding' => 'utf8', 'timezone' => 'UTC', 'flags' => , 'cacheMetadata' => true, 'log' => false,
Generate the Scaffolding Code
From the “bookmarks” directory, run the following commands to create the scaffolding code needed for the application:
bin/cake bake all users bin/cake bake all bookmarks bin/cake bake all tags
Once this the above is done, you should be able to start the server. For my setup, the vm I’m using is on a different IP address than my locahost, so I needed to specify a specific IP address and port. This is the command that I ran:
bin/cake -H 192.168.0.100:88
Then in a web browser, enter the URL ‘http://192.168.0.100:88/bookmarks’.