Open avsej opened 2 years ago
Ah, so couchbase/couchbase 4.0
is now published on packagist: https://packagist.org/packages/couchbase/couchbase, so I can pull it with composer.
This wasn't the case when we released, so probably explains why I couldn't pull it.
yeah, I had to hit update button there, also packagist only contains the pure php part, extension still have to be installed
@RichardSmedley this demo still works, maybe we could use it in the docs somehow?
I think turning it into a blog post is a great idea - and we can then link from both https://docs.couchbase.com/php-sdk/current/hello-world/start-using-sdk.html & https://docs.couchbase.com/php-sdk/current/hello-world/platform-help.html
Putting it in the docs directly would raise problems of maintenance, atm.
Getting Started with Couchbase and Composer
In this small note, I demonstrate how to use Couchbase PHP SDK with Composer.
Here I assume, that
composer
command is available in the PATH. To check that, try to display its version:Now, lets create new project. Here I'm going to use [Slim framework][slim-home], as one of the many minimalistic PHP web framework avaialble in the ecosystem.
This command will create new directory
couchbase-demo
with skeleton of the project.Now navigate in the terminal to the directory and try to start webserver
Default application will respond with
"Hello world!"
message:Lets add Couchbase SDK to the project. To do so, first we need to install the SDK in the system, SDK documentation describes it in details.
Once SDK installed, it should be displayed in the list of modules:
Next step is to update
composer.json
in the project.And run
composer update
to lock the change incomposer.lock
.The default Slim skeleton uses
InMemoryUserRepository
as an implementation of the persistence. Lets replace it with class, that will keep user records in Couchbase.Create
src/Infrastructure/Persistence/User/CouchbaseUserRepository.php
with the following content:Couchbase is new dependency for the application, so it have to be registered in application container. I decided to register it as
Couchbase\Scope::class
, becase I'm going to keep all data for the application in the same scope.Add new section to the
app/settings.php
Again, for simplicity in my case, I don't specify name of the scope here, because the repository is going to use default scope, but in real-world application, the scope name should be also set in the configuration.
Now, update
app/dependencies.php
, where we will create connection to Couchbase using credentials from the settings, and open default scope of the given bucket:And the last step is to replace in-memory implementation of the User repository with the one we mentioned previously:
That's all we need. Let's verify it now. Because the application provides read-only API, the users have to be created manually or by some other tool. The easiest way would be Couchbase Web UI. Create there couple for JSON documents like this:
id:
"1"
value:id:
"2"
value:Start the server:
Make few requests with cURL: