How to have absolute url with Hateoas Bundle on Symfony2 - symfony

I just fresh installed the bundle from Willdurant's github and I got relative url like this:
"_links": {
"self": {
"href": "/1.0/users/?page=1&limit=10"
},
"first": {
"href": "/1.0/users/?page=1&limit=10"
},
"last": {
"href": "/1.0/users/?page=2&limit=10"
},
"next": {
"href": "/1.0/users/?page=2&limit=10"
}
}
For my Hateoas url, I really prefer absolute url but I don't find anything on google to change that. 2 hours of search, trying multiple keywords and nothing...
Thanks for your help.

You can use it many ways. All depends on the way of generating links.
If you use #Route annotation to generate link, it has a parameter absolute that need to be set to true:
/**
* #Hateoas\Relation(
* name = "self",
* href = #Hateoas\Route(
* "user_get",
* parameters = { "id" = "expr(object.getId())" },
* absolute = true
* )
* )
*/
If you use expression language to generate link you can pass true as third parameter to the link() function:
/**
* #Hateoas\Relation(
* "user",
* href = "expr(link(object.getUser(), 'self', true))"
* )
*/

Related

How can i write this JSON swagger-php below with annotations?

I'm using OpenAPI 3.0 to document my Symfony API. this is the JSON code that authenticates the user to send requests:
"securitySchemes": {
"Bearer": {
"type": "http",
"description": "Entrer le token JST",
"scheme": "bearer",
"bearerFormat": "JWT"
}
}
},
"security": [
{
"Bearer": []
}
]
How can i write this with annotations in controller ?
thanks
You almost can; currently swagger-php does not support the bearerFormat property.
=> https://github.com/zircote/swagger-php/issues/1258
The rest would look something like this globally.
/**
* #OA\SecurityScheme(
* securityScheme="bearerAuth",
* type="http",
* scheme="bearer",
* description="Entrer le token JST"
* )
*/
For a controller to require security you'd add this:
/**
* #OA\Get(
* path="/api/endpoint",
* ...
* security={{ "bearerAuth": {} }}
* )
*/

Alfresco dashlet post form

I have been searching google for a few days now and cant find any solution to my problem.
I have created a custom Alfresco Aukai dashlet and placed a form in it thus:
define(["dojo/_base/declare",
"dijit/_WidgetBase",
"alfresco/core/Core",
"alfresco/core/I18nUtils",
"alfresco/dashlets/Dashlet"],
function(declare, AlfCore, I18nUtils, Dashlet) {
// First define a form...
var form = {
name: "alfresco/forms/Form",
config: {
showOkButton: true,
okButtonLabel: "Save",
showCancelButton: false,
cancelButtonLabel: "Doesn't Matter",
okButtonPublishTopic: "PUBLISH_TOPIC",
okButtonPublishGlobal: true,
widgets: []
}
};
// Define a text box...
var textBox = {
name: "alfresco/forms/controls/DojoValidationTextBox",
config: {
fieldId: "EMAIL",
name: "email",
label: "Contact",
description: "Your e-mail address",
placeHolder: "name#address.com",
validationConfig: [
{
validation: "regex",
regex: "^([0-9a-zA-Z]([-.w]*[0-9a-zA-Z])*#([0-9a-zA-Z][-w]*[0-9a-zA-Z].)+[a-zA-Z]{2,9})$",
errorMessage: "Valid E-mail Address Required"
}
]
}
};
form.config.widgets.push(textBox);
// Define a checkbox...
var checkbox = {
name: "alfresco/forms/controls/DojoCheckBox",
config: {
fieldId: "SHOW",
name: "showEmail",
label: "Show E-mail",
description: "Uncheck to hide the e-mail field",
value: true
}
};
form.config.widgets.push(checkbox);
// Update the textbox to respond to checkbox changes...
textBox.config.visibilityConfig = {
initialValue: true,
rules: [
{
targetId: "SHOW",
is: [true]
}
]
};
return declare([Dashlet], {
/*
* Add padding to the body.
* smallpad (4px padding), mediumpad (10px padding - recommended) and largepad (16px padding)
*/
additionalCssClasses: "mediumpad",
/**
* Explicit height in pixels of the Dashlet body.
*/
bodyHeight: 200,
/**
* Id that will be used to store properties for this Dashlet.
* i.e. the Dashlet height when using the resizer.
*/
componentId: "component.messaging-dashlet",
/**
* The i18n scope to use for this Dashlet.
*/
i18nScope: "dashlets.MessagingDashlet",
/**
* An array of the i18n files to use with this Dashlet.
*/
i18nRequirements: [{i18nFile: "./i18n/MessagingDashlet.properties"}],
/**
* The widgets to be acting as title bar actions.
*/
widgetsForTitleBarActions: [
{
id: "MESSAGING_DASHLET_ACTIONS",
name: "alfresco/html/Label",
config: {
label: "Title-bar actions"
}
}
],
/**
* The widgets to be placed in the top toolbar.
*/
widgetsForToolbar: [
{
id: "MESSAGING_DASHLET_TOOLBAR",
name: "alfresco/html/Label",
config: {
label: "Toolbar"
}
}
],
/**
* The widgets to be placed in the body of the dashlet.
*/
widgetsForBody: [
{
id: "HELLO_DASHLET_VERTICAL_LAYOUT",
name: "alfresco/layout/VerticalWidgets",
config: {
widgetWidth: 50,
widgets: [
form
]
}
}
]
});
});
The form is displayed but the Save button is inactive.
When checking with fire bug I am informed that there is no javascript on the page!
Apart from this every thing works fine even get the error message when an invalid email address is entered.
Any suggestions? Alternatively a working example of a form in a dashlet posting would be nice :)
I have looked into this further while trying to populate a select box with the OptionsService.
optionsConfig: {
publishTopic: "ALF_GET_FORM_CONTROL_OPTIONS",
publishPayload: {
url: url.context + "/proxy/alfresco/api/people",
itemsAttribute: "people",
labelAttribute: "firstName",
valueAttribute: "userName"
}
In the debug I can see this but nothing happens over the wire, there is not corresponding request sent to the server!
This is the same issue I am getting with the post so this would suggest a more fundamental problem with my code.
Your form does not have any widgets / controls defined.
okButtonPublishGlobal: true,
widgets: []
Please refer simple CRUD example using aikau, http://www.codingfreaks.net/2015/03/aikau-form-example-for-simple-student.html
https://github.com/Alfresco/Aikau/blob/develop/aikau/src/main/resources/alfresco/reports/TopSiteContributorReport.js
Can you please look at this link?
Alfresco share is using Site Contributor Breakdown Dashlet, with the input form controls using aikau.

Duplicate default WP REST API (v2) endpoints

How can i duplicate default wp rest api V2 endpoints? I would like to keep default endpoints and routs intact, but would like to use simplified responses for my application.
Wordpress v4.7
My code for now:
function register_custom_routes()
{
$controller = new MY_REST_Posts_Controller;
$controller->register_routes();
}
add_action( 'rest_api_init', 'register_custom_routes', 1 );
class MY_REST_Posts_Controller extends WP_REST_Controller {
// this is a copy of default class WP_REST_Posts_Controller
}
Calling http://localhost/wp/wp-json/ list my namespace ( /myrest ), also
http://localhost/wp/wp-json/myrest/gives me:
{
"namespace": "myrest",
"routes": {
"/myrest": {
"namespace": "myrest",
"methods": [
"GET"
],
...
"/myrest/(?P<id>[\\d]+)": {
"namespace": "myrest",
"methods": [
"GET",
"POST",
"PUT",
"PATCH",
"DELETE"
],
...
}
but when i try to list posts with http://localhost/wp/wp-json/myrest/posts (like with default api route call) it does not work:
{
"code": "rest_no_route",
"message": "No route was found matching the URL and request method",
"data": {
"status": 404
}
}
I need simplified version of get posts response for android app but also want to keep default rest endpoints and routs as is.
Here is the solution. I've wrapped code in wp plugin.
class WP_REST_custom_controller extends WP_REST_Controller {
// this is a copy of default class WP_REST_Posts_Controller
// Edited constructor for cutom namespace and endpoint url
/**
* Constructor.
*
* #since 4.7.0
* #access public
*
* #param string $post_type Post type.
*/
public function __construct() {
$this->post_type = 'post';
$this->namespace = 'custom_namespace/v1';
$obj = get_post_type_object( $post_type );
$this->rest_base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name;
$this->resource_name = 'posts';
$this->meta = new WP_REST_Post_Meta_Fields( $this->post_type );
}
// this is a copy of default class WP_REST_Posts_Controller with necessary edits
}
// Function to register our new routes from the controller.
function register_custom_rest_routes() {
$controller = new WP_REST_custom_controller();
$controller->register_routes();
}
add_action( 'rest_api_init', 'register_custom_rest_routes');

JMS #Discriminator filed doesn't appear if specific group is serializing

I'm using Symfony 2.8, FOSRestBundle and JMSSerializerBundle.
Problem
Discriminator field type of Document entity doesn't apear in serialized model when I serialize specific group ("api" group in folowing example) of entity Citizen.
Doctrine Entities
Document:
namespace MyBundle\Entity;
use JMS\Serializer\Annotation as JMS;
…
/**
* #JMS\Discriminator(field = "type", map = {
* "doc1" = "MyBundle\Entity\Document1",
* "doc2" = "MyBundle\Entity\Document2"
* })
*/
class Document
…
Citizen:
class Citizen
{
…
/**
* #var ArrayCollection
*
* #ORM\OneToMany(
* targetEntity="MyBundle\Entity\Document",
* cascade={ "PERSIST", "REMOVE" },
* orphanRemoval=true,
* mappedBy="citizen"
* )
*
* #JMS\Groups({"api"})
*/
private $documents;
…
What I get
{
…
"documents": [
{
"number": "000000",
"date": "01.01.1970",
"serial": "0000",
"place": ""
}
],
…
}
What I need
{
…
"documents": [
{
"type": "doc1",
"number": "000000",
"date": "01.01.1970",
"serial": "0000",
"place": ""
}
],
…
}
If I remove specific serialization group, then type field is present in serialized output.
Thanks in advance
Just found issue on github.
Seems for now, workaround with Default group is needed, see lordelph's comment

Multiple links in Bazinga Hateoas with Symfony

I am using Bazinga Hateoas with Fosrest in one of my SF2 project.
In one of API call, I want to display link of friends with current user or user id supplied like this:
{
"_links": {
"self": { "href": "/users/1" },
"friends": [
{ "href": "/users/2" },
{ "href": "/users/3" },
]
},
}
I am using below code in Entity.User.yml file:
relations:
-
rel: self
href:
route: api_1_get_users
parameters:
id: expr(object.getId())
absolute: true
-
rel: expr(object.findFriends(object.getId()))
href:
route: api_1_get_users
parameters:
id: expr(object.getId())
absolute: true
I have put "findFriends" method in repository but its not accessible inside yml file. I guess this is not the correct way of doing things.
I have gone through https://github.com/willdurand/Hateoas but not able to figure out how to do it. Please guide me how I can achieve this...
Any help would be much appreciated !
Please guide me how I can achieve this
This is how you work with #RelationProvider.
/**
* Note:
* ====
* RelationProvider takes the method name which returns the relations.
*
* #Hateoas\RelationProvider("addRelations")
*/
class LinkContainingResource
{
public function addRelations($object, ClassMetadataInterface $classMetadata)
{
/**
* Important Note:
* ===============
* Relation is actually an Hateoas\Configuration\Relation object,
* NOT \Hateoas\Configuration\Annotation\Relation
*/
return [new Relation('relation_name', 'link1'),
new Relation('relation_name', 'link2'),
new Relation('relation_name', 'link3')];
}
}
Json/Hal Result:
{
"_links": {
"relation_name": [
{"href": "link1"},
{"href": "link2"},
{"href": "link3"}
]
}
}

Resources