Reputation: 2619
I'm working on a website using Symfony 2 in which each user will have a profile page reachable at domain.com/username
(like many social websites offer).
To achieve this I need to check if the wanted URL is available when a user wants to pick one. I would like to know if there is a way in Symfony to get all URL used by routes defined in routing configuration to automatically forbid those routes as usernames.
Thank you
Upvotes: 3
Views: 2081
Reputation: 12740
Now I know that I have system routers starting with application_frontend
and application_backend
.
app/config/routing.yml
application_frontend:
resource: "@ApplicationFrontendBundle/Controller"
prefix: /
type: annotation
application_backend:
resource: "@ApplicationBackendBundle/Controller"
prefix: /backend
type: annotation
I'm using my controllers as services but you don't have to.
controller.yml
services:
application_frontend.controller.profile:
class: Application\FrontendBundle\Controller\ProfileController
arguments:
- @router
- @doctrine_common_inflector
doctrine_common_inflector:
class: Doctrine\Common\Inflector\Inflector
You should refactor explode
bit and do some cleaning such as remove action
from the route names. I left it little dirty for you to see what exactly I'm doing there. Based on your application, this code might change. It just gives you an idea how it can be done.
ProfileController
use Doctrine\Common\Inflector\Inflector;
use Symfony\Component\Routing\RouterInterface;
# .......
/**
* @Route("profile", service="application_frontend.controller.profile")
*/
class ProfileController extends Controller
{
private $router;
private $inflector;
public function __construct(
RouterInterface $router,
Inflector $inflector
) {
$this->router = $router;
$this->inflector = $inflector;
}
/**
* @Method({"GET"})
* @Route("/step_one")
*/
public function stepOneAction()
{
$list = [];
$myRoutes = ['application_frontend', 'application_backend'];
$routeCollection = $this->router->getRouteCollection();
foreach ($routeCollection->all() as $key => $value)
{
$data = $value->getDefaults();
if (isset($data['_controller'])) {
foreach ($myRoutes as $routePrefix) {
if (strstr($data['_controller'], $routePrefix) !== false) {
$routeParts = explode(':', $data['_controller']);
$routeParts2 = explode('.', $routeParts[0]);
$list[] = end($routeParts2). '/' . $this->inflector->tableize(end($routeParts));
}
}
}
}
echo '<pre>';
print_r($list);
exit;
}
}
You can compare list against app/console debug:router
command.
Result
Array
(
[0] => country/index_action
[1] => country/list_action
[2] => default/index_action
[3] => default/list_action
[4] => profile/step_one_action
[5] => profile/step_two_action
[6] => profile/step_one_process_action
[7] => profile/step_two_process_action
[8] => security/index_action
[9] => security/login_action
[10] => security/logout_action
[11] => api/json_action
[12] => api/xml_action
[13] => api/json_xml_action
[14] => default/index_action
[15] => default/list_action
[16] => default/secret_action
[17] => superadmin/index_action
[18] => superadmin/list_action
[19] => twitter/list_action
[20] => twitter/create_tweet_action
[21] => user/list_action
[22] => user/create_action
)
Upvotes: 3