Get Info Api Request - api-platform.com

currently i'm struggle with the identifier because i not need it.
i only need an get request without anything and returning some system infos.
/**
* #ApiResource(
* itemOperations={
* "info"={
* "method"="GET",
* "path"="/system/info",
* "controller"=GetInfo::class,
* "read"=false
* }
* },
* collectionOperations={
*
* }
* )
*/
thats my current config but it always requires an identifier.

It sounds as if you're after not an ApiPlatform resource, but simply a standard endpoint.
Have a look at this documentation from Symfony on how to define a route.
To me it sounds like you're after something like this:
config/routes.yaml
system_info:
path: /system/info
method: GET
controller: App\System\Info
With controller:
namespace App\System;
class Info
{
public function __invoke(): JsonResponse
{
return new JsonResponse(['pc' => 'master race']);
}
}
Remember that ApiPlatform is supposed to be working with Resources (aka: DTO's and Entities), and as you're use-case does not do either, you end up struggling to make it work.

Related

Problem with ApiPlatform SearchFilter returning all results

* #ApiFilter(SearchFilter::class, properties={"serial": "partial"})
I have the following on a products entity. The problem is, when I make API call with ?serial= it returns ALL items in the DB, obviously should have only returned one
EDIT:
This is caused by using a custom controller for the GET method. Looks like it needs refactorring.
* collectionOperations={
* "get"={
* "controller"=DeviceGetCollectionController::class,
* },
Bypasses the searchFilter
Solution was add a check in the custom controller:
if($request->get('serial')){
return $query->findBy(['company' => $user->getCompany(),'serial'=>$request->get('serial')]);
}

How can I "validate" DELETE request in api-platform

I want to check the entity variable and check if it is allowed to delete the entity. For example if the owner entity of the association is linked to another entity, I want to make the deletion impossible.
I've looked in the documentation of api-platform bu I could not find any help regarding my problems. Either you give the right to delete or not. I could not find how to control it (equivalent to validation for POST, PUT and PATCH).
You can use the access control feature of Api-Platform and Symfony Expression Language to achieve what you want. This way you can write pretty complex expressions.
I hope this example makes it clear.
user is the currently logged in user.
object is the resource user is trying to delete.
/**
* #ApiResource(
* itemOperations={
* "delete"={
* "access_control"="is_granted('ROLE_USER') and object.getUsers().contains(user),
* }
* }
* )
*/
class Entity
{
/**
* #var ArrayCollection
*
* #ORM\OneToMany(targetEntity="User", inversedBy="entities")
* #ORM\JoinTable(name="entity_users")
*/
private $users;
/**
* #return ArrayCollection
*/
public function getUsers(): ArrayCollection
{
return $this->users;
}
}
In this case only users who are stored in users Array of Entity can delete this resource.

Overriding property name in Api Platform

Oke, so I have the following use case. On some of my entities I use a file entity for example with the organization logo.
Now I want users to post either a link (I will then async get the file) or a base64 has of the file. But when the user does a get I want to present an JSON representation of the file entity (that also includes size, a thumbnail link etc).
The current setup that I have is two different properties on my entity, one for reading and one for posting with different logic. And then an event listener that handels the logic. That’s all fine and all but it causes the user to post a postLogo property in their json file, I would hower like them to post to a logo property in their json file.
Is there an annotation that I can use (for example name on ApiProperty) to achieve this or do I need to override the serializer?
/**
* #var File The logo of this organisation
*
* #ORM\ManyToOne(targetEntity="File")
* #ApiProperty(
* attributes={
* "openapi_context"={
* "type"="#/components/schemas/File"
* }
* }
* )
* #Groups({"read"})
*/
public $logo;
/**
* #var string The logo of this organisation, a logo can iether be posted as a valid url to that logo or a base64 reprecentation of that logo.
*
* #ApiProperty(
* attributes={
* "openapi_context"={
* "type"="url or base64"
* }
* }
* )
* #Groups({"write"})
*/
public $postLogo;
You can add a setter with a SerializedName annotation. Something like this should work
/**
* #Groups({"write"})
* #SerializedName("logo")
*
*/
public function setPostLogo($value)
{
$this->postLogo = $value;
}

Find error line in AngularJS app

I just started to play with AngularJS and I got error below.
Error: Argument '?' is not a function, got Object
at assertArg (http://localhost/angular/project/scripts/vendor/angular.js:1039:11)
at assertArgFn (http://localhost/angular/project/scripts/vendor/angular.js:1049:3)
at http://localhost/angular/project/scripts/vendor/angular.js:4802:9
at http://localhost/angular/project/scripts/vendor/angular.js:4384:17
at forEach (http://localhost/angular/project/scripts/vendor/angular.js:137:20)
at nodeLinkFn (http://localhost/angular/project/scripts/vendor/angular.js:4369:11)
at compositeLinkFn (http://localhost/angular/project/scripts/vendor/angular.js:4015:15)
at compositeLinkFn (http://localhost/angular/project/scripts/vendor/angular.js:4018:13)
at publicLinkFn (http://localhost/angular/project/scripts/vendor/angular.js:3920:30)
at update (http://localhost/angular/project/scripts/vendor/angular.js:14202:11)
Now, my question is: Is there a way I can find line in .js file where error occurred?
I get line number in angular.js file on raised exception but there is too many files where
error can occur.
I tried with AngularJS Batarang, but this is more for debugging semantic not syntax errors.
Thanks.
It'll be easier if you link to the js files that would have caused this error.
From the angular.js source, https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js
it looks like a problem with instantiating the controller.
Here's the line that's causing assertion failure:
/**
* #ngdoc function
* #name ng.$controller
* #requires $injector
*
* #param {Function|string} constructor If called with a function then it's considered to be the
* controller constructor function. Otherwise it's considered to be a string which is used
* to retrieve the controller constructor using the following steps:
*
* * check if a controller with given name is registered via `$controllerProvider`
* * check if evaluating the string on the current scope returns a constructor
* * check `window[constructor]` on the global `window` object
*
* #param {Object} locals Injection locals for Controller.
* #return {Object} Instance of given controller.
*
* #description
* `$controller` service is responsible for instantiating controllers.
*
* It's just a simple call to {#link AUTO.$injector $injector}, but extracted into
* a service, so that one can override this service with {#link https://gist.github.com/1649788
* BC version}.
*/
return function(constructor, locals) {
if(isString(constructor)) {
var name = constructor;
constructor = controllers.hasOwnProperty(name)
? controllers[name]
: getter(locals.$scope, name, true) || getter($window, name, true);
======> assertArgFn(constructor, name, true);
}
return $injector.instantiate(constructor, locals);
};
It's unable to find the constructor for the controller.

What is the best way to setup a view plugin in Zend Framework 2?

I'm trying to setup a view plugin to expose the route matches in Zend Framework 2.
The plugin is something like this:
class GetRouteMatch extends AbstractHelper
{
/**
* Route match returned by the router.
*
* #var RouteMatch.
*/
protected $routeMatch;
/**
* Set route match returned by the router.
*
* #param RouteMatch $routeMatch
* #return self
*/
public function setRouteMatch(RouteMatch $RouteMatch)
{
$this->routeMatch = $RouteMatch;
return $this;
}
public function __invoke($param)
{
return $this->routeMatch->getParam($param, false);
}
}
What is the best way to setup the RouteMatch object?
I have to do it in the module bootstrap or in the controller?
For the moment I've resolved this way inside the controller action
$renderer = $this->getLocator()->get('Zend\View\Renderer\PhpRenderer');
$routeMatch = $renderer->plugin('routeMatch');
$routeMatch->setRouteMatch($this->getEvent()->getRouteMatch());
The RouteMatch object is injected manually.. but I'm sure there's a better way
The best is to initialize such code in your module class. You can attach an event there to inject dependencies like the routematch. However, the routematch will be available to inject soon. There is work in progress to set the routematch in a service locator. This means you can configure DI it will pull the routematch from the service locator. Then you don't need to write these things yourself anymore.

Resources