5. Routing¶
OpenjetResourceBundle ships with a custom route loader that can save you some time.
5.1. Generating Generic CRUD Routing¶
To generate a full CRUD routing, simply configure it in your app/config/routing.yml:
openjet_user:
resource: |
alias: openjet.user
type: openjet.resource
Results in the following 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}
5.2. Using a Custom Path¶
By default, Openjet will use a plural form of the resource name, but you can easily customize the path:
openjet_user:
resource: |
alias: openjet.user
path: account
type: openjet.resource
Results in the following routes:
$ bin/console debug:router | grep user
------------------------ --------------- -------- ------ -------------------------
Name Method Scheme Host Path
------------------------ --------------- -------- ------ -------------------------
openjet_user_show GET ANY ANY /account/{id}
openjet_user_index GET ANY ANY /account/
openjet_user_create GET|POST ANY ANY /account/new
openjet_user_update GET|PUT|PATCH ANY ANY /account/{id}/edit
openjet_user_delete DELETE ANY ANY /account/{id}
5.3. Generating API CRUD Routing¶
To generate a full API-friendly CRUD routing, add these YAML lines to your app/config/routing.yml:
openjet_user:
resource: |
alias: openjet.user
type: openjet.resource_api
Results in the following routes:
$ bin/console debug:router | grep user
------------------------ --------------- -------- ------ -------------------------
Name Method Scheme Host Path
------------------------ --------------- -------- ------ -------------------------
openjet_api_user_show GET ANY ANY /users/{id}
openjet_api_user_index GET ANY ANY /users/
openjet_api_user_create POST ANY ANY /users/
openjet_api_user_update PUT|PATCH ANY ANY /users/{id}
openjet_api_user_delete DELETE ANY ANY /users/{id}
5.4. Excluding Routes¶
If you want to skip some routes, simply use except configuration:
openjet_user:
resource: |
alias: openjet.user
except: ['delete', 'update']
type: openjet.resource
Results in the following 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
5.5. Generating Only Specific Routes¶
If you want to generate only some specific routes, simply use the only configuration:
openjet_user:
resource: |
alias: openjet.user
only: ['show', 'index']
type: openjet.resource
Results in the following routes:
$ bin/console debug:router
------------------------ --------------- -------- ------ -------------------------
Name Method Scheme Host Path
------------------------ --------------- -------- ------ -------------------------
openjet_user_show GET ANY ANY /users/{id}
openjet_user_index GET ANY ANY /users/
5.6. Generating Routing for a Section¶
Sometimes you want to generate routing for different “sections” of an application:
openjet_admin_user:
resource: |
alias: openjet.user
section: admin
type: openjet.resource
prefix: /admin
openjet_account_user:
resource: |
alias: openjet.user
section: account
only: ['show', 'index']
type: openjet.resource
prefix: /account
The generation results in the following routes:
$ bin/console debug:router | grep user
------------------------ --------------- -------- ------ -------------------------
Name Method Scheme Host Path
------------------------ --------------- -------- ------ -------------------------
openjet_admin_user_show GET ANY ANY /admin/users/{id}
openjet_admin_user_index GET ANY ANY /admin/users/
openjet_admin_user_create GET|POST ANY ANY /admin/users/new
openjet_admin_user_update GET|PUT|PATCH ANY ANY /admin/users/{id}/edit
openjet_admin_user_delete DELETE ANY ANY /admin/users/{id}
openjet_account_user_show GET ANY ANY /account/users/{id}
openjet_account_user_index GET ANY ANY /account/users/
5.7. Using Custom Templates¶
By default, ResourceController will use the templates namespace you have configured for the resource.
You can easily change that per route, but it is also easy when you generate the routing:
openjet_admin_user:
resource: |
alias: openjet.user
section: admin
templates: Admin/User
type: openjet.resource
prefix: /admin
Following templates will be used for actions:
:app/Resources/views/Admin/User:show.html.twig:app/Resources/views/Admin/User:index.html.twig:app/Resources/views/Admin/User:create.html.twig:app/Resources/views/Admin/User:update.html.twig
5.8. Using a Custom Form¶
If you want to use a custom form:
openjet_user:
resource: |
alias: openjet.user
form: AppBundle/Form/Type/AdminUserType
type: openjet.resource
create and update actions will use AppBundle/Form/Type/AdminUserType form type.
Note
Remember, that if your form type has some dependencies you have to declare it as a service and tag with name: form.type.
5.9. Using a Custom Redirect¶
By default, after successful resource creation or update, Openjet will redirect to the update route and fallback to index if it does not exist.
If you want to change that behavior, use the following configuration:
openjet_user:
resource: |
alias: openjet.user
redirect: show
type: openjet.resource
5.10. API Versioning¶
One of the ResourceBundle dependencies is JMSSerializer, which provides a useful functionality of object versioning. It is possible to take an advantage of it almost out of the box. If you would like to return only the second version of your object serializations, use the following snippet:
openjet_user:
resource: |
alias: openjet.user
serialization_version: 2
type: openjet.resource_api
What is more, you can use a path variable to dynamically change your request. You can achieve this by setting a path prefix when importing file or specify it in the path option.
openjet_user:
resource: |
alias: openjet.user
serialization_version: $version
type: openjet.resource_api
Note
Remember that a dynamically resolved users prefix is no longer available when you specify path, and it has to be defined manually.
5.11. Using a Custom Criteria¶
Sometimes it is convenient to add some additional constraint when resolving resources. For example, one could want to present a list of all users from some group (which id would be a part of path). Assuming that the path prefix is /groups/{groupId}, if you would like to list all users from this group, you could use the following snippet:
openjet_user:
resource: |
alias: openjet.user
criteria:
group: $groupId
type: openjet.resource
prefix: /groups
Which will result in the following routes:
$ bin/console debug:router | grep user
------------------------ --------------- -------- ------ ---------------------------------------
Name Method Scheme Host Path
------------------------ --------------- -------- ------ ---------------------------------------
openjet_user_show GET ANY ANY /groups/{groupId}/users/{id}
openjet_user_index GET ANY ANY /groups/{groupId}/users/
openjet_user_create GET|POST ANY ANY /groups/{groupId}/users/new
openjet_user_update GET|PUT|PATCH ANY ANY /groups/{groupId}/users/{id}/edit
openjet_user_delete DELETE ANY ANY /groups/{groupId}/users/{id}
5.12. Using a Custom Identifier¶
As you could notice the generated routing resolves resources by the id field. But sometimes it is more convenient to use a custom identifier field instead, let’s say a code (or any other field of your choice which can uniquely identify your resource).
If you want to look for users by email, use the following configuration:
openjet_user:
resource: |
identifier: email
alias: openjet.user
criteria:
email: $email
type: openjet.resource
Which will result in the following routes:
$ bin/console debug:router | grep user
------------------------ --------------- -------- ------ -------------------------
Name Method Scheme Host Path
------------------------ --------------- -------- ------ -------------------------
openjet_user_show GET ANY ANY /users/{email}
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/{email}/edit
openjet_user_delete DELETE ANY ANY /users/{email}
5.13. Configuration Reference¶
openjet_user:
resource: |
alias: openjet.user
path: account
identifier: email
criteria:
email: $email
section: admin
templates: :User
form: AppBundle/Form/Type/SimpleUserType
redirect: create
except: ['show']
only: ['create', 'index']
serialization_version: 1
serialization_groups: [Default]
type: openjet.resource