Home > Blockchain >  Why using symfony route annotations doesn't work?
Why using symfony route annotations doesn't work?

Time:06-10

I'm trying to use this route



/** 
*@Route("/random/string/{length<\d>?5}", name="string")
*/
public function getString(Request $request, $length)
    {
        $string = "";
        for ($i=0; $i < $lenght; $i  ) { 
            $string .= chr(random_int(65, 90)); // adds a random char to the string
        }
        str_shuffle( $string );
        return new Response("Random string : $string");
    }


I've imported this

use Symfony\Component\Routing\Annotation\Route;

and i've ran

composer require annotations

when i run debug router or try to follow this route i get the error message :

[critical] Uncaught Error: syntax error, unexpected identifier " ", expecting "function" or "const"

CodePudding user response:

This is the right format. If still it is not working then I think the syntax error is in import statement in your class file.

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/random/string/{length<\d >?5}", name="string")
 */
public function getString(int $length, Request $request)
{
    $string = "";
    for ($i=0; $i < $lenght; $i  ) {
        $string .= chr(random_int(65, 90)); // adds a random char to the string
    }
    str_shuffle( $string );
    return new Response("Random string :". $string);
}
  • Related