Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a prefix to many routes in Symfony 4

I'd like all my Controller action routes in the namespace 'App\Controller\Api' to have the prefix '/api'. I also want to use annotations inside the controllers to set the rest of the route.

In Symfony 3 this was done by editing 'config/routing.yml':

app:
    resource: '@AppBundle/Controller/Api'
    type: annotation
    prefix: /api

How can I do this in Symfony 4? Do I need to make a bundle? Which config file would I use since I don't have a 'config/routing.yml'?

like image 945
ken Avatar asked Oct 21 '25 05:10

ken


2 Answers

Ok It looks like I should have just tried real path names. The following worked in 'config/routes.yaml':

api:
    prefix: /api
    resource: '../src/Controller/Api'
like image 163
ken Avatar answered Oct 23 '25 18:10

ken


At first run composer require annotations and then

use Symfony\Component\Routing\Annotation\Route;

/**
* @Route("/blog")
*/
class BlogController extends Controller
{

}
like image 38
Mohammad Zare Moghadam Avatar answered Oct 23 '25 17:10

Mohammad Zare Moghadam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!