Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save heavensloop/8ecc687174acd5b422df62478e8ac204 to your computer and use it in GitHub Desktop.
Save heavensloop/8ecc687174acd5b422df62478e8ac204 to your computer and use it in GitHub Desktop.
Symfony: Update Deprecate Route/Method annotations for Symfony 5.2

Upgrading Symfony Route/Method Annotations for Symfony 5.x.

In the feat to update Route annotations from the deprecated Sensio\Bundle\FrameworkExtraBundle\Configuration\Method and Sensio\Bundle\FrameworkExtraBundle\Configuration\Method to use Symfony\Component\Routing\Annotation\Route.

First of all I fixed the routes cos I realized I had some routes that ended with double closing bracket: ")".

The Fix:

Replace: (\* @Route\(.*\))\) With: $1

Next, run a regex replace to update your route annotations:

Replace: ((\*\s)@Route\((.*))\)([\s]+\*(.*))?(\s+\*\s(@Method\(\{?(("\w+"(, )?)+)\}?\)))

With: $1, methods={$8})$4

This helps you process replacements as shown below:

From:

 /**
     * Reset the password for a user (only use-able by super-admins)
     *
     * @Route("/{id}/confirm", name="user_reset")
     * @Method({"GET", "POST"})
     *
     * @param \Symfony\Component\HttpFoundation\Request $request
     * @param int $id
     */

To:

/**
     * Reset the password for a user (only use-able by super-admins)
     *
     * @Route("/{id}/confirm", name="user_reset", methods={"GET", "POST"})
     *
     * @param \Symfony\Component\HttpFoundation\Request $request
     * @param int $id
     */

From:

 /**
     * Delete the user
     *
     * @Route("/{id}/delete", name="user_reset")
     * @template()
     * @Method({"GET"})
     *
     * @param \Symfony\Component\HttpFoundation\Request $request
     * @param int $id
     */

To:

/**
     * Delete the user
     *
     * @Route("/{id}/delete", name="user_reset", methods={"GET"})
     * @template()
     * @param \Symfony\Component\HttpFoundation\Request $request
     * @param int $id
     */

If like me, you have to update over 150 controllers that have one or more routes, then you'll find this useful if you haven't already started.

IDEs like VSCode will always show you a preview of what the replacements will look like before you click replace. Cheers!!

@heavensloop
Copy link
Author

Have you ever had to do this? tell me how you did yours

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment