08/07/2018 - SYMFONY
Bu, farklı klasörlerdeki kontrolörler için rotaları tanımladığımız başka bir örnektir. Composer ile sensio/framework-extra-bundle
kütüphanesini eklemeyi unutmayın.
Bir API tasarladığımızı ve sürüm başına farklı rota yapısına sahip olduğumuzu varsayalım.
src/
├── Controller
│ └── DefaultController.php
├── v1
│ └── Controller
│ └── DefaultController.php
└── v2
└── Controller
└── DefaultController.php
services:
_defaults:
autowire: true
autoconfigure: true
public: false
App\:
resource: '../src/*'
exclude: '../src/{Kernel.php}'
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
home:
resource: ../src/Controller/
type: annotation
prefix: /
api_v1:
resource: ../src/v1/Controller/
type: annotation
prefix: /api/v1/
api_v2:
resource: ../src/v2/Controller/
type: annotation
prefix: /api/v2/
---------------------- -------- -------- ------ ----------
Name Method Scheme Host Path
---------------------- -------- -------- ------ ----------
app_default_index GET ANY ANY /
app_v1_default_index GET ANY ANY /api/v1/
app_v2_default_index GET ANY ANY /api/v2/
---------------------- -------- -------- ------ ----------
declare(strict_types=1);
namespace App\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
/**
* @Route("")
*/
class DefaultController
{
/**
* @Route("")
* @Method("GET")
*/
public function index()
{
return new Response('Home');
}
}
declare(strict_types=1);
namespace App\v1\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
/**
* @Route("")
*/
class DefaultController
{
/**
* @Route("")
* @Method("GET")
*/
public function index()
{
return new Response('Home v1');
}
}
declare(strict_types=1);
namespace App\v2\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
/**
* @Route("")
*/
class DefaultController
{
/**
* @Route("")
* @Method("GET")
*/
public function index()
{
return new Response('Home v2');
}
}