3. Configuring Your Resources

Now you need to configure your first resource. Let’s assume you have a User entity in your application and it has simple fields:

  • id
  • firstName
  • lastName
  • email

3.1. Implement the ResourceInterface in your model class.

<?php

/*
 * This file is part of the Openjet package.
 *
 * (c) Openjet SA
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Openjet\Component\User\Model;

use Openjet\Component\Resource\Model\ResourceInterface;
use Openjet\Component\Resource\Traits\IdentifiableTrait;

class User implements ResourceInterface
{
    // Most of the time you have the code below already in your class.
    use IdentifiableTrait;
}

3.2. Configure the class as a resource.

In your app/config/config.yml add:

openjet_resource:
    resources:
        user:
            classes:
                model: Openjet\Component\User\Model\User

That’s it! Your User entity is now registered as Openjet Resource.

3.3. You can also configure several doctrine drivers.

Note

Remember that the doctrine/orm driver is used by default.

openjet_resource:
    drivers:
        - doctrine/orm
        - doctrine/phpcr-odm
    resources:
        user:
            classes:
                model: Openjet\Component\User\Model\User
        customer:
            driver: doctrine/phpcr-odm
            classes:
                model: Openjet\Component\Customer\Model\CustomerDocument

3.4. Generate API routing.

Add the following lines to app/config/routing.yml:

openjet_user:
    resource: |
        alias: openjet.user
    type: openjet.resource_api

After that a full JSON/XML CRUD API is ready to use. Sounds crazy? Spin up the built-in server and give it a try:

$ bin/console server:run

You should see something like:

Server running on http://127.0.0.1:8000

Quit the server with CONTROL-C.

Now, in a separate Terminal window, call these commands:

$ curl -i -X POST -H "Content-Type: application/json" -d '{"firstName": "Oliver", "lastName": "Queen", "email": "oliver.queen@gmail.com"}' http://localhost:8000/users/
$ curl -i -X GET -H "Accept: application/json" http://localhost:8000/users/

As you can guess, other CRUD actions are available through this API.

3.5. Generate web routing.

What if you want to render HTML pages? That’s easy! Update the routing configuration:

openjet_user:
    resource: |
        alias: openjet.user
    type: openjet.resource

This will generate routing for HTML views.

Run the debug:router command to see available routes:

$ bin/console debug:router | grep user

------------------------ --------------- -------- ------ -------------------------
Name                     Method          Scheme   Host   Path
------------------------ --------------- -------- ------ -------------------------
openjet_user_show            GET             ANY      ANY    /users/{id}
openjet_user_index           GET             ANY      ANY    /users/
openjet_user_create          GET|POST        ANY      ANY    /users/new
openjet_user_update          GET|PUT|PATCH   ANY      ANY    /users/{id}/edit
openjet_user_delete          DELETE          ANY      ANY    /users/{id}

You can configure more options for the routing generation but you can also define each route manually to have it fully configurable. Continue reading to learn more!