Multi optional parameters in route symfony - symfony

I have route like this:
* #Route(
* "/search/{ro}/{mind}/{his}",
* name="search",
* requirements={"ro"="[a-zA-Z]{2,}", "mind"="()|new|old", "his"="()|yes|ok|no"}
* )
if I want this url: /search/yes I need to enter this address: /search///yes to show page.
how can remove // from original url?

welcome to stackoverflow,
you know you can have multiple routes for one action?
Just be aware of the order of the routes - the more parameters the higher - or a lesser route will catch first.
* #Route(
* "/search/{ro}/{mind}/{his}",
* name="search",
* requirements={"ro"="[a-zA-Z]{2,}", "mind"="()|new|old", "his"="yes|ok|no"}
* )
* #Route(
* "/search/{his}",
* name="search_short",
* requirements={"his"="yes|ok|no"}
* )

Related

Swagger annotation for query parameters for file upload

I am trying to make an api documentation for my symfony based api and I would like to add info about the upload endpoint for my api. I am using nelmioapidocbundle. Can anyone please provide a link to the documentation where I can find it? or better if there is an example. Thank you.
/**
* #Route("/products/{id}/images", methods={"POST"})
*
* #SWG\Parameter(
* name="file",
* in="body",
* type="file",
* ---- Im not sure what to put here to make this work
* )
*
* #SWG\Response(
* response=201,
* description="File is uploaded."
* )
* )
*/
public function uploadImages(Request $request, FileUploadService $fileUploadService) {}
For anyone who might encounter this. this formData in value fixed the problem:
* #SWG\Parameter(
* name="file",
* in="formData", <----
* required=true,
* type="file",
* description="product image"

How to define an associative array in Nelmio API DOC vs 3?

I am using Symfomy 3.4 with Nelmio API DOC vs 3.
I have a GET API with a multiplier parameter of the format:
multiplier[14]=2&multiplier[15]=1. I want to be able to make the request from /api-doc panel with the keys of the array, not only the values. At this moment, I can add only values.
The way I am defining the relationship at this moment is:
* #SWG\Parameter(
* name="multiplier",
* in="query",
* type="array",
* description="multiply the received reward",
* #SWG\Items(
* type="integer",
* ),
* required=false
* )
I tried several things, like
* #SWG\Property(
* type="string"
* ),
but it seems not to work.
How can I make the key appear in the panel so I can complete it there, without using something external, such as Postman?

Symfony unique integer fields in entity

I have entity with uniq field, inviteCode. And when I create new entity I want set automatic some random code, but this code must be different from exist in db, what do you thing, what practices about that you know ?
/**
* #ORM\Table(name="users")
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
* #AssertBridge\UniqueEntity(
* groups={"registration"},
* fields="inviteCode",
* errorPath="not valid",
* message="This inviteCode is already in use."
* )
*/
class User extends AbstractUser implements UserInterface
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=500, unique=true)
* #Annotation\SerializedName("_invite_code")
*/
private $inviteCode;
I found something like that
private function calculateReference($number)
{
$multipliers = array(7,3,1);
$length = strlen($number);
$numberArr = str_split($number);
$sum = 0;
for ($i = $length - 1; $i >= 0; --$i)
{
$sum += $numberArr[$i] * $multipliers[($length - 1 - $i) % 3];
}
return $number.(10 - $sum % 10) % 10;
}
first get max id from table then call function calculateReference with id and then setInviteCode.
But I believe doctrine have something exist for this issue or maybe somebody have good example for this
Someone provided a great answer here https://stackoverflow.com/a/13917309/4173130.
But like he said at the end, you don't need doctrine for such a simple feature. Generating the code in the constructor is an efficient, simple and clear solution.
You can use a UUID library like ramsey/uuid. Then you would be able to generate any random code with Uuid::uuid4();.
Another solution is to use random_bytes() with base64_encode : base64_encode(random_bytes(32)).
Please don't try to create a new function to generate random values. Most of time it is not secure, see https://www.owasp.org/index.php/Insecure_Randomness.
Why not using a uuid? It is included in php as a core function and i believe it suits your needs.
Check in the official documentation here

Create slug with relationships

I would like to create a slug with the combinations of relations of external tables. I would like to take 2 values ​​from a table and another value from un'atra table. I tried to do this but I do not understand how to add two values ​​and another table .. and it is required to have the fields (in my case empty)?
/**
* #ORM\OneToOne(targetEntity="Acme\UserBundle\Entity\User", inversedBy="profile")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
* */
private $utente;
/**
* #Gedmo\Slug(handlers={
* #Gedmo\SlugHandler(class="Gedmo\Sluggable\Handler\RelativeSlugHandler", options={
* #Gedmo\SlugHandlerOption(name="relationField", value="utente"),
* #Gedmo\SlugHandlerOption(name="relationSlugField", value="nickname"),
* #Gedmo\SlugHandlerOption(name="separator", value="-")
* })
* }, separator="-", updatable=true, fields={"empty"})
* #ORM\Column(length=64, unique=true)
*/
private $slug;
I would also email as well as username field,and add another relationField

Set 'parameters' annotation in NelmioApiDocBundle

I create an API RESTfull using Symfony2.1 with FOSRESTBundle and I am using NelmioApiDocBundle to generate automatic documentation.
I have a PUT request in which the user should send one parameter, but I don't need to create a Form for this purpose. All works perfectly but when I generate the documentation I don't know how to add this parameter to the documentation because I don't have a 'input' form.
I tried this but seems doesn't work:
/**
* #ApiDoc(
* description="description",
* statusCodes={
* 200="Success",
* 400="Bad request"},
* parameters={
* {"name"="parameter_name", "dataType"="integer"}
* }
* )
...
In the documentation of NelmioApiDocBundle I didn't see any solution for this...
use filters key instead of parameters
* filters={
* {"name"="parameter_name", "dataType"="integer"},
* },

Resources