Proper way of doing it (example):
Create src/Controller/BarController.php
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
class BarController
{
public function index()
{
return new Response('<p>Bar controler response</p>');
}
}
and src/Controller/FooController.php
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
class FooController
{
public function index()
{
return new Response('<p>Foo controler response</p>');
}
}
Create config/routes/prefix-routes.yaml
index:
path: /
controller: App\Controller\DefaultController::index
bar:
path: /bar
controller: App\Controller\BarController::index
foo:
path: /foo
controller: App\Controller\FooController::index
and edit routing config/routes.yaml
- delete its contents and just put:
prefixed:
resource: "routes/prefix-routes.yaml"
prefix: service1
all of controllers are now available at urls:
http://localhost/service1/ for DefaultController.php
http://localhost/service1/bar for BarController.php
http://localhost/service1/foo for FooController.php
If you want your profiler to work with service1
prefix as well then edit config/routes/dev/web_profiler.yaml
this way:
web_profiler_wdt:
resource: '@WebProfilerBundle/Resources/config/routing/wdt.xml'
prefix: service1/_wdt
web_profiler_profiler:
resource: '@WebProfilerBundle/Resources/config/routing/profiler.xml'
prefix: service1/_profiler
now they should be available at:
http://localhost/service1/_wdt... for wdt
http://localhost/service1/_profiler for profiler
Adding prefix for annotations:
Create controller src/Controller/AnnoController.php
:
<?php
namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
class AnnoController extends AbstractController
{
/**
* @Route("/anno", name="anno")
*/
public function index()
{
return new Response('<p>Anno controler response</p>');
}
}
edit config/routes/annotations.yaml
and add prefix: service1
:
controllers:
resource: ../../src/Controller/
type: annotation
prefix: service1
kernel:
resource: ../../src/Kernel.php
type: annotation
Now prefix is added to routes done via annotation:
http://localhost/service1/anno for AnnoController.php
Some references:
Symfony Routing Prefix
Symfony Routing Configuration Keys
Adding prefix quick and dirty workaround to add prefix service1
to all of routing (not recommended).
Instead of changing routing as above just Edit src/Kernel.php
protected function configureRoutes
and change every $routes->import
line by adding ->prefix('service1')
at the end so it looks this way:
protected function configureRoutes(RoutingConfigurator $routes): void
{
$routes->import('../config/{routes}/'.$this->environment.'/*.yaml')->prefix('service1');
$routes->import('../config/{routes}/*.yaml')->prefix('service1');
if (is_file(\dirname(__DIR__).'/config/routes.yaml')) {
$routes->import('../config/{routes}.yaml')->prefix('service1');
} elseif (is_file($path = \dirname(__DIR__).'/config/routes.php')) {
(require $path)($routes->withPath($path), $this);
}
}
all of controllers are now available at urls:
http://localhost/service1/ for DefaultController.php
http://localhost/service1/bar for BarController.php
http://localhost/service1/foo for FooController.php
as well as the profiler:
http://localhost/service1/_wdt... for wdt
http://localhost/service1/_profiler for profiler