Skip to content

[Routing] Route Name Prefixing #19612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chrisguitarguy opened this issue Aug 14, 2016 · 9 comments
Closed

[Routing] Route Name Prefixing #19612

chrisguitarguy opened this issue Aug 14, 2016 · 9 comments

Comments

@chrisguitarguy
Copy link
Contributor

This is a feature request/suggestion.

It would pretty nifty to be able to prefix route IDs/names on import. The idea is that the same routing resource could be imported multiple times and generate unique route names.

For example: if you have a typical form login on one host for a UI and a stateless API token authentication on another. You might want to share the same routing config for your API between the two hosts.

<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/routing
        http://symfony.com/schema/routing/routing-1.0.xsd">

    <!-- ... -->

    <import resource="@AppBundle/Resources/config/api.xml" prefix="/api" host="%uihost%">
        <default key="_format">json</default>
    </import>

    <import resource="@AppBundle/Resources/config/api.xml" host="%apihost%">
        <default key="_format">json</default>
    </import>

</routes>

Right now this can't be done. The second import overrides all the named routes in the first. Ideally I'd like to be able to do this...

<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/routing
        http://symfony.com/schema/routing/routing-1.0.xsd">

    <!-- ... -->

    <import resource="@AppBundle/Resources/config/api.xml" prefix="/api" host="%uihost%">
        <default key="_format">json</default>
    </import>

    <import resource="@AppBundle/Resources/config/api.xml" host="%apihost%" idprefix="api.">
        <default key="_format">json</default>
    </import>

</routes>

All routes in the second import would get the ID api.{original_id_attribute_value}.

@Perni1984
Copy link

+1 for having that feature in the routing component. I have a similar use case here, where I need to import some routes from bundles for different hosts with different prefixes, thus the last import wins (because the route name is the same).

Currently an alternative approach could be to

  • use the FOSRestBundle which implements name_prefix: see here -> just adding the whole FOSRestBundle for this requirement seems a bit too much
  • copy the routing files into one big routing files and change the route names by hand -> not really DRY

@chalasr
Copy link
Member

chalasr commented Sep 15, 2016

The use case seems legit to me.
Would it be problematic to provide this in the core regarding FOSRestBundle? IMHO Route name prefixing looks useful in lots of situations, it doesn't look like a REST-specific utility.

@anyt
Copy link

anyt commented Jan 24, 2017

If you have enabled FOSRestBundle in your application you can use name_prefix with "type: rest" with any route collections, it's not necessary to use FOS routing loader only with only rest route collections.

Or if you don't have this bundle, you still can add custom routing loader like FOS does.

@mmoreram
Copy link
Contributor

mmoreram commented Aug 7, 2017

@alexhoma and me will develop this feature.

@nicolas-grekas
Copy link
Member

@mmoreram @alexhoma PR pending ;)

@sroze
Copy link
Contributor

sroze commented Nov 27, 2017

I can have a go at that, I'd like to have something similar as well.

@sroze
Copy link
Contributor

sroze commented Nov 27, 2017

Here we go: #25178 :)

@stof stof added this to the 4.1 milestone Nov 27, 2017
@fabpot fabpot closed this as completed Dec 2, 2017
fabpot added a commit that referenced this issue Dec 2, 2017
…ation (sroze)

This PR was squashed before being merged into the 4.1-dev branch (closes #25178).

Discussion
----------

[Routing] Allow to set name prefixes from the configuration

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #19612
| License       | MIT
| Doc PR        | ø

This allows setting name prefixes to routes while importing them. Typically, we can then import multiple times a similar file. This was originally requested by 🎸 @chrisguitarguy in #19612

```yaml
app:
    resource: ../controller/routing.yml

api:
    resource: ../controller/routing.yml
    name_prefix: api_
    prefix: /api
```

```xml
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/routing
        http://symfony.com/schema/routing/routing-1.0.xsd">

    <import resource="../controller/routing.xml" />
    <import resource="../controller/routing.xml" prefix="/api" name-prefix="api_" />

</routes>
```

Commits
-------

880d7e7 [Routing] Allow to set name prefixes from the configuration
@devnix
Copy link

devnix commented Nov 3, 2018

I think I have a bug related with this feature.
I have a route group for an admin section, and I want to prefix the URL and name of each route inside the App\Controller\Admin namespace.

If I go only for the route prefix it works without any problem

# config/routes.yaml
admin:
  prefix: /admin
  resource: ../src/Controller/Admin
  type: annotation

The router debug gives me the following output:

 -------------------------- ---------- -------- ------ ----------------------------------- 
  Name                       Method     Scheme   Host   Path                               
 -------------------------- ---------- -------- ------ ----------------------------------- 
  _twig_error_test           ANY        ANY      ANY    /_error/{code}.{_format}           
  _wdt                       ANY        ANY      ANY    /_wdt/{token}                      
  _profiler_home             ANY        ANY      ANY    /_profiler/                        
  _profiler_search           ANY        ANY      ANY    /_profiler/search                  
  _profiler_search_bar       ANY        ANY      ANY    /_profiler/search_bar              
  _profiler_phpinfo          ANY        ANY      ANY    /_profiler/phpinfo                 
  _profiler_search_results   ANY        ANY      ANY    /_profiler/{token}/search/results  
  _profiler_open_file        ANY        ANY      ANY    /_profiler/open                    
  _profiler                  ANY        ANY      ANY    /_profiler/{token}                 
  _profiler_router           ANY        ANY      ANY    /_profiler/{token}/router          
  _profiler_exception        ANY        ANY      ANY    /_profiler/{token}/exception       
  _profiler_exception_css    ANY        ANY      ANY    /_profiler/{token}/exception.css   
  index                      ANY        ANY      ANY    /admin/                            
  login                      ANY        ANY      ANY    /admin/login                       
  user_index                 GET        ANY      ANY    /admin/user                        
  user_new                   GET|POST   ANY      ANY    /admin/user/new                    
  user_show                  GET        ANY      ANY    /admin/user/{id}                   
  user_edit                  GET|POST   ANY      ANY    /admin/user/{id}/edit              
  user_delete                DELETE     ANY      ANY    /admin/user/{id}                   
  admin_logout               ANY        ANY      ANY    /admin/logout                      
 -------------------------- ---------- -------- ------ ----------------------------------- 

The problem comes when I add the name_prefix route:

# config/routes.yaml
admin:
  name_prefix: admin_
  prefix: /admin
  resource: ../src/Controller/Admin
  type: annotation

The router debug gives the following output, and I can verify that the URLs are duplicated:

 -------------------------- ---------- -------- ------ ----------------------------------- 
  Name                       Method     Scheme   Host   Path                               
 -------------------------- ---------- -------- ------ ----------------------------------- 
  index                      ANY        ANY      ANY    /                                  
  login                      ANY        ANY      ANY    /login                             
  user_index                 GET        ANY      ANY    /user                              
  user_new                   GET|POST   ANY      ANY    /user/new                          
  user_show                  GET        ANY      ANY    /user/{id}                         
  user_edit                  GET|POST   ANY      ANY    /user/{id}/edit                    
  user_delete                DELETE     ANY      ANY    /user/{id}                         
  _twig_error_test           ANY        ANY      ANY    /_error/{code}.{_format}           
  _wdt                       ANY        ANY      ANY    /_wdt/{token}                      
  _profiler_home             ANY        ANY      ANY    /_profiler/                        
  _profiler_search           ANY        ANY      ANY    /_profiler/search                  
  _profiler_search_bar       ANY        ANY      ANY    /_profiler/search_bar              
  _profiler_phpinfo          ANY        ANY      ANY    /_profiler/phpinfo                 
  _profiler_search_results   ANY        ANY      ANY    /_profiler/{token}/search/results  
  _profiler_open_file        ANY        ANY      ANY    /_profiler/open                    
  _profiler                  ANY        ANY      ANY    /_profiler/{token}                 
  _profiler_router           ANY        ANY      ANY    /_profiler/{token}/router          
  _profiler_exception        ANY        ANY      ANY    /_profiler/{token}/exception       
  _profiler_exception_css    ANY        ANY      ANY    /_profiler/{token}/exception.css   
  admin_index                ANY        ANY      ANY    /admin/                            
  admin_login                ANY        ANY      ANY    /admin/login                       
  admin_user_index           GET        ANY      ANY    /admin/user                        
  admin_user_new             GET|POST   ANY      ANY    /admin/user/new                    
  admin_user_show            GET        ANY      ANY    /admin/user/{id}                   
  admin_user_edit            GET|POST   ANY      ANY    /admin/user/{id}/edit              
  admin_user_delete          DELETE     ANY      ANY    /admin/user/{id}                   
  admin_logout               ANY        ANY      ANY    /admin/logout                      
 -------------------------- ---------- -------- ------ ----------------------------------- 

@Tobion
Copy link
Contributor

Tobion commented Nov 4, 2018

@devnix please open a new issue for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests