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'?

ken
  • 161
  • 1
  • 8
  • You can still import routes and set a prefix without a bundle. https://stackoverflow.com/questions/48120939/symfony-4-global-route-prefix/48121299#48121299 – Cerad Jan 18 '18 at 03:38
  • I saw that page. I don't understand where Resources comes from or what I need in my 'config/routes.yaml'. I started my project with `composer create-project symfony/skeleton my-project` – ken Jan 18 '18 at 03:56
  • Don't actually need a Resources directory in this case. Just make a config/routes_api.yaml file and import it. And your 4.1 link applies to the name of the route not the route itself. So not applicable. – Cerad Jan 18 '18 at 04:32
  • @Cerad I failed to mention I want to use annotations to build the route cache. Is config/routes_api.yaml intended to contain routes? – ken Jan 18 '18 at 04:46

2 Answers2

9

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'
ken
  • 161
  • 1
  • 8
3

At first run composer require annotations and then

use Symfony\Component\Routing\Annotation\Route;

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

}
  • 2
    This is a great example, but It only shows me how to make a single route with the yaml file. I'd like to use the config file to set the prefix for many controllers in a particular namespace and use annotations to set the rest of the route. – ken Jan 18 '18 at 05:05
  • 1
    This does work, but then I'm forced to type @Route('/api') manually for every route controller that needs. I even tried extending a class with the prefix and it did not carry over. – ken Jan 18 '18 at 22:13