I can't find any info about the global route prefix in a Symfony 4 application. The only thing I've found is annotating the controllers with @route
. But I don't use annotations and I need all the controllers to have the same prefix.
Now I could do that in S3 like this in the app/config/routing.yml
file:
app:
resource: '@AppBundle/Controller/'
type: annotation
prefix: /foo
But S4 is bundleless and I can't do the same - I would have to create a bundle which I don't need.
Is it possible to define a global route prefix in Symfony 4 at all or I'm going to end up with prefixing every single root/creating a custom route loader, especially if I configure routes in YAML?
This works with annotations, giving every controller 'this/prefix' :
# file: config/routes/annotations.yaml
controllers:
resource: ../../src/Controller/
type: annotation
prefix: this/prefix
Then for imported routes 'this/prefix' gets applied to all of FOSUserBundle's routes below (yes I tested it).
# file: config/routes.yaml
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
prefix: this/prefix
If you are not using annotations you should be able to import your separate routing file (which can import many more) applying the prefix to everything in the resource. But drop the "@" notation and use relative path to your file. The value of 'this/prefix' can be configured as a Service Container Parameter , look here: http://symfony.com/doc/current/routing/service_container_parameters.html
I'm probably not understanding the question because I don't know why you would want a global prefix in your app. You can set a prefix when importing a route file even if you are using yaml and no bundles.
# config/routes.yaml
blog:
resource: '../src/Resources/config/routes/blog.yaml'
prefix: blog
# src/Resources/config/routes/blog.yaml
blog_show:
path: /show
controller: App\Controller\BlogController::show
blog_list:
path: /list
controller: App\Controller\BlogController::list
bin/console debug:router would yield
blog_show ANY ANY ANY /blog/show
blog_list ANY ANY ANY /blog/list
But again, I suspect you are asking for something else. Perhaps you could add an example to your question?
There are several ways to achieve prefix to any route. They differ mainly due to the route type they target.
For routes defined by annotations you need to edit config/routes/annotations.yaml
and put there prefix: your-prefix
key-value pair.
For routes defined by YAML list you need to edit config/routes.yaml
put there under a new route prefix: your-prefix
key-value pair and resource: "routes/routes-that-you-want-to-be-prefixed.yaml"
and create the file routes/routes-that-you-want-to-be-prefixed.yaml
that has all the routes then remove all routes from config/routes.yaml
besides that one which uses resource: "routes/routes-that-you-want-to-be-prefixed.yaml"
.
You may also use quick and dirty approach if you're in hurry and want to check prefixing. Edit src/Kernel.php
and suffix all $routes->import(..)
with ->prefix('your-prefix)
(no need to change any routing files)
I wrote a comprehensive description with some example code samples as an aswer to that question: Running Symfony 5 with reverse proxy in subdirectory
Example code snippets have been tested against Symfony 4.4 and Symfony 5.1 and worked without any problem.
I have the same issue and haven't been able to solve it using yaml configuration but I have it working adding an annotation on the top of the class like this:
/**
*
* Rest\Route("/api")
*/
class TestController extends FosRestController
{
/**
* @Rest\Route("/test", name="tm_test")
* @Method({"GET"})
*/
public function testAction()
{
...
}
}
That will produce a route /api/test
for the testAction()
Look this post: https://symfony.com/blog/new-in-symfony-3-4-prefix-all-controller-route-names
Hope it helps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.