3

Basically what i want to achieve is this:

I have this paths:

http://SITENAME/dashboard

http://SITENAME/users

And they are mapped to the right controllers and actions:

/**
 * @Route("/dashboard")
 */
public function dashboardAction()
{
    // handle the request
}

/**
 * @Route("/users")
 */
public function usersAction()
{
    // handle the request
}

Now I want to make these other paths to map to the same controllers and actions:

http://SITENAME/{companyName}/dashboard

http://SITENAME/{companyName}/users

companyName is a name that I check on db if exists and if not throw a NotFound Exception.

These paths will just add some filter to the queries made, basically showing the same data structure.

I know I can create other actions and put those after the previous ones in order to be catched but I'm asking if there's something clever... something like this:

/**
 * @Route("/({companyName}/)dashboard")
 */
public function dashboardAction($companyName = null)
{
    // handle the request
}

where companyName is optional.

Thanks...

UPDATE

As Manolo suggested I already tried something like this and it works:

/**
 * @Route("/dashboard")
 * @Route("/{companyName}/dashboard")
 */
public function dashboardAction($companyName = null)
{
    // handle the request
}

But I think there's a clever way of handling it...

3
  • What about a paramconverter for the companyName? Commented Dec 10, 2015 at 15:42
  • Hi Matteo. Would you explain what do you mean? Thanks... Commented Dec 10, 2015 at 16:02
  • 1
    Hi, i try with an answer, Commented Dec 10, 2015 at 16:07

3 Answers 3

2

Look at this example: http://symfony.com/doc/current/book/routing.html#routing-with-placeholders

// src/AppBundle/Controller/BlogController.php

// ...

/**
 * @Route("/blog/{page}", defaults={"page" = 1})
 */
public function indexAction($page)
{
    // ...
}

When page is not defined in the route, it is equal to 1.

But this won't work in your case, because you defined two routes for the same action:

/dasboard

/{companyName}/dashboard

So you have two options:

1. Define two routes for the same action (weird).

2. Change your defined route to: /dashboard/{companyName} (better)

Then, you can get /dashboard and /dashboard/{companyName} with the same action.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Manolo... This is standard practice but not what I need. As i wrote, the (optional) parameter, for what i need, is not a the end of the path but at the beginning: "/({companyName}/)dashboard
You're right. You may define two routes for the same action, because they are two different routes: /{companyName}/dashboard and /dashboard.
Anyway, I recommend you to use /dashboard/{companyName}. It's more consistent.
Thanks Manolo. I've already done as you suggest (two different routes) and it works. I just think that maybe there's a clever or better way to handle it. I will edit my question to mark what you suggest as already tried. About the path structure -> no way. This is the structure that i need to achieve and it is for several reasons that are outside of the scope. But thank you anyway!!!
Did you found any other way?
0

Try like this:

/**
 * @Route("/{companyName}/dashboard")
 */
public function dashboardAction($companyName = null)
{
    // handle the request
}

1 Comment

I already tried this, but it's not good. While it handle paths like "/company1/dashboard" it fails to handle "/dashboard" (without the optional prefix)
0

I suggest you to use the @ParamConverter annotation calls converters to convert request parameters to objects.

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

/**
 * @Route("/dashboard")
 * @Route("/({companyName}/)dashboard")
 * @ParamConverter("company", class="AcmeDemoBundle:Company", options={"name" = "companyName"})
 */
public function showAction(Company $company = null)
{

}

The double route definition permit the default null company value

Hope this help

3 Comments

Thanks for you answer. But, while your answer shows a neat feature for having direct access to the entity based on the param passed, it does not help me preventing the definition of two routes that is what i asked for. Thank you anyway.
Yes, is only an improvement, not what you are looking for. The double routing definition as mentioned by @manolo is good for you?
yes, i already tried what @manolo suggested and it works. But, as i wrote, i'm trying and searching for something different and maybe better... (if exists)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.