Skip to content

Commit 544b61e

Browse files
committed
get started pages merged
1 parent fc0c61f commit 544b61e

6 files changed

+310
-325
lines changed

source/get-started.txt

+310-8
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@ Get Started with the PHP Library
1818
:description: Learn how to create an app to connect to MongoDB deployment by using the PHP library.
1919
:keywords: quick start, tutorial, basics
2020

21-
.. toctree::
22-
23-
Download & Install </get-started/download-and-install/>
24-
Create a Deployment </get-started/create-a-deployment/>
25-
Create a Connection String </get-started/create-a-connection-string/>
26-
Run a Sample Query </get-started/run-sample-query/>
27-
Next Steps </get-started/next-steps/>
28-
2921
Overview
3022
--------
3123

@@ -44,3 +36,313 @@ Follow this guide to connect a sample PHP application to a MongoDB Atlas
4436
deployment. If you prefer to connect to MongoDB using a different driver or
4537
programming language, see our :driver:`list of official drivers <>`.
4638

39+
.. _php-download-and-install:
40+
41+
Download and Install
42+
--------------------
43+
44+
.. facet::
45+
:name: genre
46+
:values: tutorial
47+
48+
.. meta::
49+
:keywords: setup, composer, installation, code example
50+
51+
.. procedure::
52+
:style: connected
53+
54+
.. step:: Install dependencies
55+
56+
Before you begin developing, ensure that you have the following
57+
dependencies installed on your local machine:
58+
59+
- :php:`PHP <install>` version 7.4 or later
60+
- `Composer <https://getcomposer.org/download/>`__ version 2.0 or later
61+
62+
.. step:: Install the MongoDB PHP extension
63+
64+
Run the following command to install the ``mongodb`` PHP extension:
65+
66+
.. code-block:: bash
67+
68+
sudo pecl install mongodb
69+
70+
.. step:: Update your PHP configuration file
71+
72+
To enable the ``mongodb`` extension in your PHP configuration file, add the
73+
following line to the top of your ``php.ini`` file:
74+
75+
.. code-block:: none
76+
77+
extension=mongodb.so
78+
79+
.. tip::
80+
81+
You can locate your ``php.ini`` file by running the following command
82+
in your shell:
83+
84+
.. code-block:: bash
85+
86+
php --ini
87+
88+
.. step:: Create a project directory
89+
90+
From your root directory, run the following command in your shell to create
91+
a directory called ``php-quickstart`` for this project:
92+
93+
.. code-block:: bash
94+
95+
mkdir php-quickstart
96+
97+
Select the tab corresponding to your operating system and run the following commands
98+
to create a ``quickstart.php`` application file in the ``php-quickstart`` directory:
99+
100+
.. tabs::
101+
102+
.. tab:: macOS / Linux
103+
:tabid: create-file-mac-linux
104+
105+
.. code-block:: bash
106+
107+
cd php-quickstart
108+
touch quickstart.php
109+
110+
.. tab:: Windows
111+
:tabid: create-file-windows
112+
113+
.. code-block:: bash
114+
115+
cd php-quickstart
116+
type nul > quickstart.php
117+
118+
.. step:: Install the {+php-library+}
119+
120+
To install the {+php-library+}, run the following command in your ``php-quickstart``
121+
directory:
122+
123+
.. code-block:: bash
124+
125+
composer require mongodb/mongodb
126+
127+
After installing the library, include Composer's ``autoload.php`` file by adding the
128+
following code to the top of your ``quickstart.php`` file:
129+
130+
.. code-block:: php
131+
132+
<?php
133+
134+
require_once __DIR__ . '/vendor/autoload.php';
135+
136+
After you complete these steps, you have a new project directory, a
137+
new application file, and the library dependencies installed.
138+
139+
.. _php-create-deployment:
140+
141+
Create a MongoDB Deployment
142+
---------------------------
143+
144+
.. facet::
145+
:name: genre
146+
:values: tutorial
147+
148+
.. meta::
149+
:keywords: cloud, host, atlas
150+
151+
You can create a free tier MongoDB deployment on MongoDB Atlas
152+
to store and manage your data. MongoDB Atlas hosts and manages
153+
your MongoDB database in the cloud.
154+
155+
.. procedure::
156+
:style: connected
157+
158+
.. step:: Create a free MongoDB deployment on Atlas
159+
160+
Complete the :atlas:`Get Started with Atlas </getting-started>`
161+
guide to set up a new Atlas account and load sample data into a new free
162+
tier MongoDB deployment.
163+
164+
.. step:: Save your credentials
165+
166+
After you create your database user, save that user's
167+
username and password to a safe location for use in an upcoming step.
168+
169+
After you complete these steps, you have a new free tier MongoDB
170+
deployment on Atlas, database user credentials, and sample data loaded
171+
into your database.
172+
173+
.. _php-connection-string:
174+
175+
Create a Connection String
176+
--------------------------
177+
178+
.. facet::
179+
:name: genre
180+
:values: tutorial
181+
182+
.. meta::
183+
:keywords: uri, atlas
184+
185+
You can connect to your MongoDB deployment by providing a
186+
**connection URI**, also called a *connection string*, which
187+
instructs the driver on how to connect to a MongoDB deployment
188+
and how to behave while connected.
189+
190+
The connection string includes the hostname or IP address and
191+
port of your deployment, the authentication mechanism, user credentials
192+
when applicable, and connection options.
193+
194+
.. TODO:
195+
To connect to an instance or deployment not hosted on Atlas, see
196+
:ref:`php-connection-targets`.
197+
198+
.. procedure::
199+
:style: connected
200+
201+
.. step:: Find your MongoDB Atlas Connection String
202+
203+
To retrieve your connection string for the deployment that
204+
you created in the :ref:`previous step <php-create-deployment>`,
205+
log in to your Atlas account and navigate to the
206+
:guilabel:`Database` section and click the :guilabel:`Connect` button
207+
for your new deployment.
208+
209+
.. figure:: /includes/figures/atlas_connection_select_cluster.png
210+
:alt: The connect button in the clusters section of the Atlas UI
211+
212+
Then, select your user from the :guilabel:`Select database user`
213+
selection menu. Select "PHP" from the :guilabel:`Driver` selection
214+
menu and the version that best matches the version you installed
215+
from the :guilabel:`Version` selection menu.
216+
217+
Select the :guilabel:`String` tab in the :guilabel:`Add connection string into your application code`
218+
step to view only the connection string.
219+
220+
.. step:: Copy your Connection String
221+
222+
Click the button on the right of the connection string to copy it
223+
to your clipboard, as shown in the following screenshot:
224+
225+
.. figure:: /includes/figures/atlas_connection_copy_string_php.png
226+
:alt: The copy button next to the connection string in the Atlas UI
227+
228+
.. step:: Update the Placeholders
229+
230+
Paste this connection string into a file in your preferred text editor
231+
and replace the ``<username>`` and ``<password>`` placeholders with
232+
your database user's username and password.
233+
234+
Save this file to a safe location for use in the next step.
235+
236+
After completing these steps, you have a connection string that
237+
corresponds to your Atlas cluster.
238+
239+
.. _php-run-sample-query:
240+
.. _php-connect-to-mongodb:
241+
242+
Run a Sample Query
243+
------------------
244+
245+
.. facet::
246+
:name: genre
247+
:values: tutorial
248+
249+
.. meta::
250+
:keywords: test connection, runnable, code example
251+
252+
After retrieving the connection string for your MongoDB Atlas deployment,
253+
you can connect to the deployment from your PHP application and query
254+
the Atlas sample datasets.
255+
256+
.. procedure::
257+
:style: connected
258+
259+
.. step:: Edit your PHP application file
260+
261+
Copy and paste the following code into the ``quickstart.php`` file, which queries
262+
the ``movies`` collection in the ``sample_mflix`` database:
263+
264+
.. literalinclude:: /includes/get-started/quickstart.php
265+
:language: php
266+
:dedent:
267+
268+
.. step:: Assign the connection string
269+
270+
Assign the ``MONGODB_URI`` environment variable to the connection string that you copied
271+
from the :ref:`php-connection-string` step of this guide. You can assign this
272+
variable by running a shell command or creating a ``.env`` file in your application,
273+
as show in the following tabs:
274+
275+
.. tabs::
276+
277+
.. tab:: Shell Command
278+
:tabid: shell
279+
280+
.. code-block:: sh
281+
282+
export MONGODB_URI=<connection string>
283+
284+
.. tab:: .env File
285+
:tabid: dotenv
286+
287+
.. code-block:: none
288+
289+
MONGODB_URI=<connection string>
290+
291+
.. step:: Run your PHP application
292+
293+
In your project directory, run the following shell command to start the application:
294+
295+
.. code-block:: bash
296+
297+
php quickstart.php
298+
299+
The command line output contains details about the retrieved movie
300+
document:
301+
302+
.. code-block:: none
303+
:copyable: false
304+
305+
{
306+
"_id": {
307+
"$oid": "..."
308+
},
309+
...
310+
"rated": "R",
311+
"metacritic": 80,
312+
"title": "The Shawshank Redemption",
313+
...
314+
}
315+
316+
If you encounter an error or see no output, ensure that you assigned the
317+
proper connection string to the ``MONGODB_URI`` environment variable and
318+
that you loaded the sample data.
319+
320+
After you complete these steps, you have a PHP application that
321+
connects to your MongoDB deployment, runs a query on the sample
322+
data, and returns a matching document.
323+
324+
.. _php-next-steps:
325+
326+
Next Steps
327+
----------
328+
329+
.. facet::
330+
:name: genre
331+
:values: reference
332+
333+
.. meta::
334+
:keywords: learn more
335+
336+
Congratulations on completing the quick start tutorial!
337+
338+
.. include:: /includes/get-started/troubleshoot.rst
339+
340+
In this tutorial, you created a PHP application that
341+
connects to a MongoDB deployment hosted on MongoDB Atlas
342+
and retrieves a document that matches a query.
343+
344+
345+
Learn more about the {+php-library+} from the following resources:
346+
347+
- Learn how to perform read operations in the :ref:`<php-read>` section.
348+
- Learn how to perform write operations in the :ref:`<php-write>` section.

source/get-started/create-a-connection-string.txt

-68
This file was deleted.

0 commit comments

Comments
 (0)