forked from cakephp/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In the discussion of cakephp#3806 and some additional discussion in slack, building a new tutorial that combines the best elements of the blog tutorial and the more advanced topics from the bookmarker into a single tutorial was determined to be a good course of action. This is the start of that new tutorial. Long term we'd be dropping the blog and bookmarker tutorials from the documentation to streamline the experience for new developers.
- Loading branch information
Showing
6 changed files
with
262 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
CMS Tutorial - Creating the Articles Controller | ||
############################################### | ||
|
||
* Create the Controller | ||
* Add a index & view action. | ||
* Add the index & view template | ||
* Add add action | ||
* Add validation | ||
* Create add templates. | ||
* Add edit action. | ||
* Add edit view. | ||
* Add delete action. | ||
|
||
Next we'll be creating :doc:`basic actions for our Tags and Users tables | ||
<tags-and-users>`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CMS Tutorial - Authentication | ||
############################# | ||
|
||
* Adding login | ||
* Adding logout | ||
* Enabling Access Control | ||
* Updating Creation | ||
* Restricting Editing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
CMS Tutorial - Creating the Database | ||
#################################### | ||
|
||
Now that we have CakePHP installed, let's set up the database for our :abbr:`CMS | ||
(Content Management System)` application. If you haven't already done so, create | ||
an empty database for use in this tutorial, with a name of your choice, e.g. | ||
``cake_cms``. You can execute the following SQL to create the necessary | ||
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 articles ( | ||
id INT AUTO_INCREMENT PRIMARY KEY, | ||
user_id INT NOT NULL, | ||
title VARCHAR(255) NOT NULL, | ||
slug VARCHAR(255) NOT NULL, | ||
body TEXT, | ||
published BOOLEAN DEFAULT FALSE, | ||
created DATETIME, | ||
modified DATETIME, | ||
UNIQUE KEY (slug), | ||
FOREIGN KEY user_key (user_id) REFERENCES users(id) | ||
) CHARSET=utf8mb4; | ||
|
||
CREATE TABLE tags ( | ||
id INT AUTO_INCREMENT PRIMARY KEY, | ||
title VARCHAR(255), | ||
created DATETIME, | ||
modified DATETIME, | ||
UNIQUE KEY (title) | ||
) CHARSET=utf8mb4; | ||
|
||
CREATE TABLE articles_tags ( | ||
article_id INT NOT NULL, | ||
tag_id INT NOT NULL, | ||
PRIMARY KEY (article_id, tag_id), | ||
FOREIGN KEY tag_key(tag_id) REFERENCES tags(id), | ||
FOREIGN KEY bookmark_key(article_id) REFERENCES articles(id) | ||
); | ||
|
||
INSERT INTO users (email, password, created, modified) | ||
VALUES | ||
('[email protected]', 'sekret', NOW(), NOW()); | ||
|
||
INSERT INTO articles (user_id, title, slug, body, published, created, modified) | ||
VALUES | ||
(1, 'First Post', 'first-post', 'This is the first post.', 1, now(), now()); | ||
|
||
You may have noticed that the ``articles_tags`` table used a composite primary | ||
key. CakePHP supports composite primary keys almost everywhere, making it easier | ||
to build multi-tenanted applications. | ||
|
||
The table and column names we used were not arbitrary. By using CakePHP's | ||
:doc:`naming conventions </intro/conventions>`, we can leverage CakePHP more | ||
effectively and avoid needing to configure the framework. While CakePHP is | ||
flexible enough to accommodate almost any database schema, adhering to the | ||
conventions will save you time as you can leverage the convention based defaults | ||
CakePHP provides. | ||
|
||
Database Configuration | ||
====================== | ||
|
||
Next, let's tell CakePHP where our database is and how to connect to it. Replace | ||
the values in the ``Datasources.default`` array in your **config/app.php** file | ||
with those that apply to your setup. A sample completed configuration array | ||
might look something like the following:: | ||
|
||
return [ | ||
// More configuration above. | ||
'Datasources' => [ | ||
'default' => [ | ||
'className' => 'Cake\Database\Connection', | ||
'driver' => 'Cake\Database\Driver\Mysql', | ||
'persistent' => false, | ||
'host' => 'localhost', | ||
'username' => 'cakephp', | ||
'password' => 'AngelF00dC4k3~', | ||
'database' => 'cake_cms', | ||
'encoding' => 'utf8mb4', | ||
'timezone' => 'UTC', | ||
'cacheMetadata' => true, | ||
], | ||
], | ||
// More configuration below. | ||
]; | ||
|
||
Once you've saved your **config/app.php** file, you should see that 'CakePHP is | ||
able to connect to the database' section have a checkmark. | ||
|
||
.. note:: | ||
|
||
A copy of CakePHP's default configuration file is found in | ||
**config/app.default.php**. | ||
|
||
Creating our First Model | ||
======================== | ||
|
||
* Create the Table | ||
* Create the Entity | ||
|
||
Next, we'll create our first :doc:`Controller and Template <articles-controller>`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
Content Management Tutorial | ||
########################### | ||
|
||
This tutorial will walk you through the creation of a simple :abbr:`CMS (Content | ||
Management System)` application. . To start with, we'll be installing CakePHP, | ||
creating our database, and building simple article management. | ||
|
||
Here's what you'll need: | ||
|
||
#. A database server. We're going to be using MySQL server in this tutorial. | ||
You'll need to know enough about SQL in order to create a database, and run | ||
SQL snippets from the tutorial. CakePHP will handle building all the queries | ||
your application needs. Since we're using MySQL, also make sure that you have | ||
``pdo_mysql`` enabled in PHP. | ||
#. Basic PHP knowledge. | ||
|
||
Before starting you should make sure that you have got an up to date PHP | ||
version: | ||
|
||
.. code-block:: bash | ||
php -v | ||
You should at least have got installed PHP |minphpversion| (CLI) or higher. | ||
Your webserver's PHP version must also be of |minphpversion| or higher, and | ||
should be the same version your command line interface (CLI) PHP is. | ||
|
||
Getting CakePHP | ||
=============== | ||
|
||
.. TODO:: | ||
Should we use Oven instead? | ||
|
||
The easiest way to install CakePHP is to use Composer. Composer is a simple way | ||
of installing CakePHP from your terminal or command line prompt. First, you'll | ||
need to download and install Composer if you haven't done so already. If you | ||
have cURL installed, it's as easy as running the following: | ||
|
||
.. code-block:: bash | ||
curl -s https://getcomposer.org/installer | php | ||
Or, you can download ``composer.phar`` from the | ||
`Composer website <https://getcomposer.org/download/>`_. | ||
|
||
Then simply type the following line in your terminal from your | ||
installation directory to install the CakePHP application skeleton | ||
in the **cms** directory: | ||
|
||
.. code-block:: bash | ||
php composer.phar create-project --prefer-dist cakephp/app cms | ||
If you downloaded and ran the `Composer Windows Installer | ||
<https://getcomposer.org/Composer-Setup.exe>`_, then type the following line in | ||
your terminal from your installation directory (ie. | ||
C:\\wamp\\www\\dev\\cakephp3): | ||
|
||
.. code-block:: bash | ||
composer self-update && composer create-project --prefer-dist cakephp/app cms | ||
The advantage to using Composer is that it will automatically complete some | ||
important set up tasks, such as setting the correct file permissions and | ||
creating your **config/app.php** file for you. | ||
|
||
There are other ways to install CakePHP. If you cannot or don't want to use | ||
Composer, check out the :doc:`/installation` section. | ||
|
||
Regardless of how you downloaded and installed CakePHP, once your set up is | ||
completed, your directory setup should look something like the following:: | ||
|
||
/cms | ||
/bin | ||
/config | ||
/logs | ||
/plugins | ||
/src | ||
/tests | ||
/tmp | ||
/vendor | ||
/webroot | ||
.editorconfig | ||
.gitignore | ||
.htaccess | ||
.travis.yml | ||
composer.json | ||
index.php | ||
phpunit.xml.dist | ||
README.md | ||
|
||
Now might be a good time to learn a bit about how CakePHP's directory structure | ||
works: check out the :doc:`/intro/cakephp-folder-structure` section. | ||
|
||
Checking our Installation | ||
========================= | ||
|
||
We can quickly check that our installation is correct, by checking the default | ||
home page. Before you can do that, you'll need to start the development server: | ||
|
||
.. code-block:: bash | ||
bin/cake server | ||
.. note:: | ||
|
||
For Windows, the command needs to be ``bin\cake server`` (note the backslash). | ||
|
||
This will start PHP's built-in webserver on port 8765. Open up | ||
**http://localhost:8765** in your web browser to see the welcome page. All the | ||
bullet points should be checkmarks other than CakePHP being able to connect to | ||
your database. If not, you may need to install additional PHP extensions, or set | ||
directory permissions. | ||
|
||
Next, we will build our :doc:`Database and create our first model <database>`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
CMS Tutorial - Tags and Users | ||
############################# | ||
|
||
* Using Bake to create Users CRUD | ||
* Add password hashing, update our user | ||
* Using Bake to create Tags CRUD | ||
* Create some content | ||
* Find Articles by Tag (route, action, finder, template) | ||
* Improve Tagging | ||
|
||
Next we'll be adding :doc:`authentication <authentication>`. |